Updated July 24, 2023
Introduction to Python Substring
Substring is a term used for describing a part or a portion of a string that can be extracted from the main string. For instance, ‘stand’ is a substring of ‘understand’. In python, the slicing operation is used for picking the required substring from the main string, which is declared in the program. The syntax for substring goes like ‘string_var[start_index : end_index]’, where string_var is the variable used for declaring a string value, the start and end index values refer to the character place count of the string.
What is a Substring in Python?
Substrings are basically part of a string. Many a time, we are required to extract a portion from a string and do some operation on that. Let us cite an example to see that.
Example: “Coding in Python is fun.”
Substrings in Python
For having a substring, we need to have a string at first. Let us see an example where we first declare a string and then create a substring from that.
Code:
statement = "Coding in Python is fun."
substring = statement[0:7]
print("Original Text :",statement)
print("Substring :",substring)
Output:
In the above example, we have defined a variable named statement which is of datatype str with value “Coding in Python is fun.” The text within the double quotes signifies the value stored in the variable.
In Python, a string is stored as a sequence of characters, and we can iterate over it or select from it using the slicing operation as shown above. Before going into the details of Substring, let us first look at the syntax of creating a substring.
Syntax of Substring:
string_var[start_index : end_index]
string_var is the variable having data type as a string
start_index indicates the index value of the character in the string from which we want the substring to start
end_index indicates the index value of the character in the string until which we want the substring to be. The end_index is exclusive and is not taken into account while displaying the substring. So, the substring will not end at the character at the end_index position but at the character which is prior to the end_index position
Let us elaborate it with the help of an example:
Code:
statement = "Coding in Python is fun."
start_index = 0
end_index = 7
print("Original Text :",statement)
length_statement = len(statement)
print("Length of the string",statement,"is",length_statement)
print("Character at",start_index,"th position :",statement[start_index])
print("Character at",end_index,"th position :",statement[end_index])
substring = statement[start_index:end_index]
print("Substring :",substring)
Output:
The substring starts from “C” of Coding and ends just before “i” of in. There is a space between Coding and in, and that is where the substring ends.
Now let us look at a naïve approach of the same functionality.
Code:
statement = "Coding in Python is fun."
length_statement = len(statement)
for i in range(length_statement-1):
if(i<7):
print(statement[i], end="")
Output:
By looking at the above code, it is evident that we are iterating from 0 to length_statement-1, i.e. (24-1) = 23, and for the condition I <7, we are printing each character.
If we want to fetch a single character, we just need to specify a single index value inside the square bracket like follows:-
Code:
statement = "Coding in Python is fun."
index = 4
print("The character at the",index,"th index position :",statement[index])
Output:
Though both the start and indices are not mandatory, we mention both the start and end indices if we want to print a range of values.
Examples
If the start index is kept blank, it assumes to substring it from the start, and if the end index is kept blank, then it assumes to substring till the end.
1. End string as blank
Code:
statement = "Coding in Python is fun."
start_index = 4
print("The substring of the statement starting from the \n", start_index,"th index position till the end :",statement[start_index:])
Output:
2. Start index as blank
Code:
statement = "Coding in Python is fun."
end_index = 16
print("The substring of the statement ending at from the \n",end_index-1,"th index position from the start :",statement[:end_index])
Output:
3. Start and End index with step
Code:
statement = "Coding in Python is fun."
start_index = 0
end_index = 16
step = 2
print("The substring of the statement starting from \n",start_index,"th index position and ending at the",end_index-1,"th index position with step",step," :",statement[start_index:end_index:step])
Output:
The substring starts from the 0th position, and it prints till the 15th position with step 2. i.e. it prints every 2nd character.
4. Reversing a string using substring
Code:
statement = "Coding in Python is fun."
string_length = len(statement)
print("The reverse of the string",statement,"\n using substring :",statement[string_length::-1])
Output:
Here the start index is the length of the string, and we keep the end index blank and step as -1 to print in reverse order.
Conclusion
It is time to draw closure to this article on Python substring. Today we learned many things about Python substrings, starting from syntax to how we can get the substring using a naïve approach. Finally, we looked at many examples of how to use a substring and how to reverse a string using a substring. Now that we have learned all these, it is time for you to practice.
Recommended Articles
This is a guide to Python Substring. Here we discuss the introduction, Substrings in Python, along with the Examples, codes, and Outputs. You may also look at the following articles to learn more –