In this source code example, we will demonstrate how to use the Python string startswith() method to check if a string begins with another string.
The following shows the syntax of the startswith() method:The following example shows how to use the string startswith() method to check if a string starts with another string:The following example uses the startswith() method to check if a string starts with one of the strings in a tuple:
The following example illustrates how to use the startswith() method to check if the string starts with the word make in lowercase starting from position 14:
Python
Python String
Python string startswith() method
The startswith() method returns True if a string starts with another string. Otherwise, it returns False.The following shows the syntax of the startswith() method:
str.startswith(prefix, [,start [,end ])The startswith() method accepts three parameters:
- prefix is a string or a tuple of strings to search for. The prefix parameter is mandatory.
- start is the position where the method starts looking for the prefix. The start parameter is optional.
- end is the position in the string where the method stops searching for the prefix. The end parameter is also optional.
Python string startswith() method examples
s = 'Source Code Example'
result = s.startswith('Source')
print(result)
Output:
True
s = 'Make it work, make it right, make it fast.'
result = s.startswith(('Make','make'))
print(result)
Output:
True
s = 'Make it work, make it right, make it fast.'
result = s.startswith('make', 14)
print(result)
Output:
True
Related Python String Examples
- Python string literals
- Python string length example
- Python String join() method example
- Python string split() method example
- Python String index() method example
- Python string find() method example
- Python string startswith() method example
- Python string endswith() method example
- Python String lower() method example
- Python String upper() method example
- Python string title() method example
- Python string capitalize() method example
- Python string islower() method example
- Python string istitle() method example
- Python string isupper() method example
- Python string swapcase() method example
- Python string strip() method example
- Python string replace() method example
- Python string isdigit() method example
- Python string isdecimal() method example
- Python string isnumeric() method example
- Python string isalpha() method example
- Python string isalnum() method example
Comments
Post a Comment