Python

Python String startswith and endswith

Sometimes, we need to check the starting or the ending part of any string for the programming purpose. There are two built-in methods in Python to do the task. These are startswith() and endswith() methods. If any string starts with a given prefix then startswith() method will return true otherwise returns false and if any string ending with a given suffix then endswith() method will return true otherwise returns false. How these methods work and use in Python are shown in this tutorial. Spyder3 editor is used here to write and run the python script.

startswith() Method

You can search any sub-string from the beginning or a particular position of the string by using this method.

Syntax:

string.startswith( prefix [, start [, end]] )

Here, the prefix is the mandatory parameter of this method that will specify the substring that you want to search. The other two parameters are optional. start parameter is used to specify the starting position of the string from where the search will start and the end parameter is used to specify the ending position of the string to stop the search. The uses of this method are shown below.

Example-1: Use startswith() to Search Particular Strings

Create a Python file with the following script to know the uses of the startswith() method. In the first output, the method is called with the searching text only. In the second and third outputs, the method is called with the searching text, starting position, and ending position. In the fourth output, the method is called with a searching text of multiple words.

#!/usr/bin/env python3

# Define the text
text = "Welcome to LinuxHint"

# Check the substring exists in the zero position or not
print("Output-1:", text.startswith('Welcome'))

# Check the substring exists in the particular positions
print("Output-2:", text.startswith('Linux', 10, 16))

# Check the substring exists in the particular positions
print("Output-3:", text.startswith('Linux', 11, 16))

# Check the string of multiple words exist in the particular positions
print("Output-4:", text.startswith('come to', 3, 15))
Output:

The output is shown on the right side of the image. The first output is true because the ‘Welcome’ word exists in the variable, text. The second output is False because the word, ‘Linux’ does not exist in position 10. The third output is True because the word, ‘Linux’ exists within the position 11 to 16. The fourth output returns True because the text, ‘come to’ exists within the position 3 to 15.

Example-2: Use startswith() to Search Tuple of Strings

Create a Python file with the following script to search string in the tuple using startswith() method. Here, startswith() method is used to search string without any position, with starting position and, with starting and ending positions.

#!/usr/bin/env python3

# Define the text
text = "Python is a very popular programming language"

# Check the any string of the tuple exists in the zero position or not
print("Output-1:", text.startswith(('Python', 'popular', 'language')))

# Check the any string of the tuple exists in the particular positions
print("Output-2:", text.startswith(('very', 'programming'), 15))

# Check the any string of the tuple exists in the particular positions
print("Output-3:", text.startswith(('is', 'popular', 'language'), 7, 50))

Output:

The output is shown on the right side of the image. The first output is True because all tuple values exist in the text. The second output is False because the tuple value, ‘very’ does not exist in the position, 15. The third output is true because all tuple values exist within the range 7 to 50.

endswith() Method

endswith() method works like startswith() method but it starts searching from the end of the string.

Syntax:

string.endswith( suffix [, start [, end]] )

the suffix is a mandatory parameter here and it specifies the sub-string that will be searched from the end of the string. If you want to search from the specific position from the end of the string then you can use start and end parameters. The uses of this method are shown below.

Example-3: Use endswith() to Search Particular Strings

Create a Python file with the following script. Here, endswith() method is called for five times without position value, with only starting position value, and with both starting and ending position values.

#!/usr/bin/env python3

text = "Python is an interpreted programming language"

# Check the substring exists in the last position of the text or not
print("Output-1:", text.endswith('age'))

# Check the substring exists in the particular position
print("Output-2:", text.endswith('language', 30))

# Check the substring exists in the particular positions
print("Output-3:", text.endswith('programming', 24, 36))

# Check the string of multiple words exist in the particular positions
print("Output-4:", text.endswith('programming language', 24, 45))

# Check the string of multiple words exist in the particular positions
print("Output-5:", text.endswith('programming language', 24, 40))

Output:

The output is shown on the right side of the image. The first output is True because the string, ‘age’ exists at the end of the string. The second output is True because the string, ‘language’ exists at the end of the text if you start the search from position 30. The third output is True because the string, ‘programming’ exists at the end position if you search it from position 24 to 36.

The fourth output is True because the string, ‘programming language’ exists at the end position if you search it from position 24 to 45. The fifth output is False because the string, ‘programming language’ doesn’t exist at the end position if you search it from position 24 to 40.

Example-4: Use endswith() to Search Tuple of Strings

Create a Python file with the following code to search any string value from a tuple in a text by using endswith() method. This method is called three times in the script without position value and with the position values.

#!/usr/bin/env python3

text = "Python is an interpreted programming language"

# Check the any string of the tuple exists in the last position of the string or not
print("Output-1:", text.endswith(('Python', 'interpreted', 'language')))

# Check the any string of the tuple exists in the particular positions
print("Output-2:", text.endswith(('programming', 'language'), 20))

# Check the any string of the tuple exists in the particular positions
print("Output-3:", text.endswith(('interpreted', 'programming', 'language'), 30, 60))

Output:

The output is shown on the right side of the image. The first output is True because the string, ‘language’ exists at the end of the string. The second output is True because the string, ‘language’ exists at the end of the text if you start the search from position 20. The third output is True because none of the tuple values exists at the end position of the text if you search within the position 30 to 60.

Conclusion

It is very easy to search a particular string from the starting and the ending of a long text by using startswith() and endswith() methods in Python. I hope this tutorial will help the reader to understand the uses of these methods properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.