Are you looking to find ways in Python how you can substring a string? Python 3.0 provides a lot of ways to do substring of strings. In terms of python, it’s called slicing.
Syntax for Substring a String
To Perform or to make a substring of any given string you have to follow the below template.
string[start: end: step:]
Here “start” is the starting index of the main string from which you want the substring to start. If in your code you skip start then it will automatically consider start as 0 i.e. starting index of the main string.
The “end” here represents the last index of the main string which will be the end index of your substring. By default, if the end is not included then It will be considered as the length of the main string.
The last “step” represents the number after each insertion you want to increase. By default, it is set to 1. Hence once it adds one character it moves to another one. Suppose after adding one character you want to skip next and add another one then you have to set step as 2.
For Example:
newString = "You are on Coduber.com" print(newString[11:22:1]) ##output = Coduber.com print(newString[0:22:2]) #output = Yuaeo oue.o
Different Templates for Substring a String in Python
string[start : end]
: Slice the string in a substring from a starting index “start” to the “end”-1.string[: end]
: Slice the string in a substring from beginning to “end”-1.string[start: ]
Slice the string in a substring from a starting index “start” until the string’s end.
Please note: the “start” index can be positive as well as negative. A Negative Start index means that you want to start the substring index to start from the end of the string minus the index provided instead of simply from starting of the index plus. Similarly, A Negative End index means that you want to end the substring from the beginning of the string till the end of the string minus the index. Below are the examples for all the negative indexes as well with output.
Further Examples of Above Templates
newString = "You are on Coduber.com" #get the string between starting character 11 till the character 22-1. print(newString[11:22]) ##output = Coduber.com #get First 4 Character of a string print(newString[:4]) #output = You #get last 11 character of a string print(newString[11:]) #output = Coduber.com #get the starting substring of a string except the last 4 character. print(newString[:-4]) #output = You are on Coduber #get the substring as last 3 character of a string. print(newString[-3:]) #outpu = com #more example of negative index. print(newString[-11:-4]) #output = Coduber.com
Note that print(newString[:4])
is similar to print(newString[0:4])
. Similarly print(newString[11:])
is the same as print(newString[11:22]) the length of the string is 22 here.
Validate or Check if a substring is present within another string in Python
You can check any substring whether it’s part of a larger string or not very quickly and easily in python by using the “in” operator. Below is the template of the code you can use for example.
substring in string
Let us take an example with a working code.
newString = "You are on Coduber.com" subString = "Coduber" otherString = "guide" print(subString in newString) #Output = True print(otherString in newString) #output = False
The “in” operator return True or False based upon the comparison of whether the substring is present in the string or not. But if you want the starting index of the substring present in the string then you have to use the find function in Python. It is an inbuilt function that returns starting index of the substring in the string. If the substring is not found in the string then -1 is returned.
Let check an example to get the starting index of the substring in the string.
newString = "You are on Coduber.com" subString = "Coduber" otherString = "guide" print(newString.find(subString)) #Output = 11 print(newString.find(otherString)) #Output = -1
You should learn to run the python script as well if you do not know and want to learn.
Wrap Up
That is all for this post of how can you substring a string in python. Let us know if you have any queries or suggestions. Please subscribe to our newsletter to get the latest information as soon as it is posted on our website.
If you liked the answer then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read: