{"id":487,"date":"2022-01-15T05:48:54","date_gmt":"2022-01-15T05:48:54","guid":{"rendered":"https:\/\/techransom.com\/?p=487"},"modified":"2022-01-15T06:48:07","modified_gmt":"2022-01-15T06:48:07","slug":"ways-to-slice-the-pythons-substring","status":"publish","type":"post","link":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/","title":{"rendered":"Ways to slice the Python\u2019s substring"},"content":{"rendered":"\n
There are many ways in Python to use a string as a substring. This is often referred to as “slicing”. But if you are puzzled about the ways to do it, we are here to help you. The syntax is as follows:<\/p>\n
\nstring [start: end: step]<\/span><\/p>\n<\/blockquote>\n
Start<\/span> location: Substring start index. The characters in this index are contained in the substring. If start<\/span> is not included, it is assumed to be 0.<\/p>\n
end<\/span>: The final index of the substring.<\/span> The characters in this index are not in the substring. If the end<\/span> is not included or the specified value exceeds the length of the string, it is assumed to be the length of the string by default.<\/p>\n
Step<\/span>: Each “step” character after the current character is to be inserted. The default is 1. If no step<\/span> is included, it is considered equal to 1.<\/p>\n
\u00a0Basic use <\/strong><\/p>\n
string [start: end]<\/span>: Get all characters from the beginning(Start<\/span>) to the end<\/span> 1<\/p>\n
string [: end]<\/span>: Get all the characters from the beginning(Start<\/span>) to the end<\/span> 1<\/p>\n
string [start:]<\/span>: Get all the characters from the beginning(Start<\/span>) of the string Get all characters to the end<\/p>\n
string [start: end: step]<\/span>: Get all characters from start<\/span> to finish(end<\/span>) without step<\/span> characters<\/p>\n
You will understand it in a better manner with the help of examples.<\/p>\n
Examples<\/strong><\/p>\n
\n
- \n
Get the first 5 characters of the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n
string = “freeCodeCamp<\/span>”<\/p>\n
Print<\/span> (character string [0: 5<\/span>])<\/p><\/blockquote>\n
Output:<\/p>\n
> freeC<\/p><\/blockquote>\n
Note: print (string [: 5])<\/span> provides the same result as a print (string [0: 5])<\/span>.<\/p>\n
\n
- \n
Gets a 4 character long Python\u2019s substring starting with the 3rd character of the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n
string = “freeCodeCamp<\/span>”<\/p>\n
Print<\/span> (character string [2: 6<\/span>])<\/p><\/blockquote>\n
Edition:<\/p>\n
> eeCo<\/p><\/blockquote>\n
\n
- \n
Get the last character in the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n
string = “freeCodeCamp<\/span>”<\/p>\n
Print<\/span> (character string [-1<\/span>])<\/p><\/blockquote>\n
Edition:<\/p>\n
> p<\/p><\/blockquote>\n
Note that the start or end index can be negative. A negative index means that the count starts at the end of the string, not at the beginning of the string (right to left).<\/p>\n
Index 1 represents the last character in the string and 2 represents the penultimate character.<\/p>\n
\n
- \n
Get the last 5 characters of the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n
string = “freeCodeCamp<\/span>”<\/p>\n
Print<\/span> (character string [5:<\/span>])<\/p><\/blockquote>\n
Edition:<\/p>\n
> eCamp<\/p><\/blockquote>\n
\n
- \n
Get a substring containing all but the last 4 characters and the first character <\/strong><\/h2>\n<\/li>\n<\/ol>\n
string = “freeCodeCamp<\/span>”<\/p>\n
Print<\/span> (character string [1: 4<\/span>])<\/p><\/blockquote>\n
Output:<\/p>\n
> reeCode<\/p><\/blockquote>\n
\n
- \n
Get every other character from a string <\/strong><\/h2>\n<\/li>\n<\/ol>\n
string = “freeCodeCamp<\/span>”<\/p>\n
Print<\/span> (character string [:: 2<\/span>])<\/p><\/blockquote>\n
Output:<\/p>\n
> feCdCm<\/p><\/blockquote>\n
We hope that you can do so without any confusion and ease.<\/p>\n","protected":false},"excerpt":{"rendered":"
There are many ways in Python to use a string as a substring. This is often referred to as “slicing”….<\/p>\n","protected":false},"author":1,"featured_media":496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pc"],"yoast_head":"\n
Ways to slice the Python\u2019s substring - Tech Ransom<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\t\n\t\n\t\n