Python String Slicing – What is it?
Python string slicing will be presented in this chapter with examples to satisfy the essentials of the understanding approach.
Python String Slicing By Character
Python string can be sliced with the slice syntax to return a range of characters.
To retrieve part of a string, simply enter the start index and the end index, separated by a colon.
You need to locate the characters between positions 2 and 5 (not included):
Example
Note: Please note that the first character has an index of 0.
Python String Slicing From Start
In Python string slicing, the range will begin at the first character if the start index is not given:
You will need the characters starting at position 1 until position 5 (not included):
Example
Python String Slicing Till End
In this approach, the range will go till the last character without specifying the end index:
Obtain the characters from position 3 till the last character:
Example
Python String Reverse Indexing
For a slice starting from the string’s end, use reverse indexes:
Take a look at the characters:
Example
Another Example From “y” in “Python” (Position: -12) till “i” in “String” (Position:-2)
Example
Python String Slicing Uses
String slicing in Python allows you to extract specific portions, or substrings, from a string based on their position or index. Slicing is done using square brackets []
and provides a concise way to manipulate strings. Here are some common uses of string slicing:
- You can use string slicing to access individual characters within a string. For example,
my_string[0]
will give you the first character of the string, andmy_string[-1]
will give you the last character. This is useful when you need to perform operations or checks on specific characters in a string. - String slicing allows you to extract a portion of a string, known as a substring. For example,
my_string[2:5]
will extract characters from index 2 to index 4 (excluding index 5), giving you a substring consisting of those characters. This is helpful when you need to extract specific parts of a string for further processing. - String slicing can be used to create a copy of a string. For instance,
my_string[:]
will create a new string that is an exact copy ofmy_string
. This can be useful when you want to make modifications to a string while preserving the original. - By using negative indices in string slicing, you can easily reverse a string. For example,
my_string[::-1]
will give you the reversed version ofmy_string
. Reversing a string can be useful in certain algorithms or when you need to present data in a different order. - You can specify a step value in string slicing to skip characters. For example,
my_string[::2]
will give you every second character ofmy_string
. This can be handy for extracting alternate characters or performing operations on specific intervals within a string. - String slicing can help determine if a string is a palindrome (reads the same forwards and backwards). By comparing a string with its reversed version using
my_string == my_string[::-1]
, you can check if it remains the same when reversed. - String slicing can assist in splitting a string into multiple substrings based on a specific delimiter. For instance,
my_string.split(",")
will splitmy_string
into a list of substrings at each occurrence of the comma delimiter. This is commonly used for parsing CSV files or breaking down sentences into words.