In this tutorial, we’ll learn what Python string startswith() method is and how to properly use it. We’ll go through multiple Python code examples to understand how startswith method works.
Outline
- Introduction: Python String startswith() Method
- Syntax of startswith() Method
- Example 1: startswith() Method applied on a Python String
- Example 2: startswith() Method with Specified Range
- Example 3: Python String startswith() Method Return Value
- Conclusion
Introduction: Python String startswith() Method
This method returns True if a string starts with a specified substring and returns False, if not.
Syntax of startswith() Method
string.startswith(substring, startingPoint(optional), endingPoint(optional))
- Python string startswith method takes three arguments.
- First argument is the substring for which the string is checked that whether it starts from that substring or not.
- Second argument is optional. It’s used to specify the starting point of search. The starting point of string can be customized using it.
- Third argument is also optional. Its used to specify the ending point of search. We can limit the search range by specifying the starting and ending point.
- Python string startswith method return True if the string starts with specified substring, or else it returns False.
Example 1: startswith() Method applied on a Python String
print('This is Python'.startswith('Python')) print('This is Python'.startswith('This')) print(' Python Programming Language'.startswith('Python')) print('Python Programming'.startswith('python'))
Output
False True False False
- In example 1, the string does not starts with the specified substring so it returns False.
- In example 2, the string starts with the specified substring so it returns True.
- In example 3, the string has whitespace before substring(Python) which substring passed to startswith method don’t have, so it returns False.
- In example 4, we can see that Python string startswith method is case sensitive(P and p are different).
Example 2: startswith() Method with Specified Range
print('This is Python'.startswith('Python')) print('This is Python'.startswith('Python',8))
Output
False True
As we can see that both the strings are same. But in second example, we’ve specified a starting point which means the string will start from that specific point/index. As a result, it returns True because we’ve used the index 8 from which substring(Python) starts.
We can also specify an ending point. See below example:
print('Python Programming Language.'.startswith('Python',4,10))
Output
False
This is how Python string startswith method works. You can try it with even more examples to better understand the working of this method.
Example 3: Python String startswith() Method Return Value
print('Python Programming Language.'.startswith('Python', 0, 10))
Output
True
We can see that Python string startswith method has returned True as the string starts with specified substring.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly use Python string startswith method. I’d be very happy to receive your feedback on this post. Thank you for reading it.
You may like to read:
How To Properly Use Python String RSplit Method [Detailed Python Guide]
How To Use Python String RStrip Method – Easy Python Code Examples
How To Easily Use Python String RPartition Method [Detailed Python Guide]
How To Easily Use Python String Split Method [Detailed Python Guide]
How To Easily Use Python String RFind Method [Detailed Python Guide]
How To Use Python String IsLower() Method – Easy Python Code Examples
How To Use Python String Isidentifier() Method – Easy Python Code Guide
How To Use Python String LStrip Method – Easy Python Code Examples