When working with strings in programming, you may wish to check whether a string begins with a specific value. For example, suppose you’re writing a program that checks to see if a user’s name begins with a specific letter in order to carry out a special lottery event or receive a discount. This is where the Python startswith() function comes into play. Let us dive deep into this article to understand the Python startswith() concept.
Definition
- Python startswith() is a string method that returns True if the input string starts with the specified prefix(string); else it returns False.
- The Python startswith() string function is used to check whether the input string starts with the specified string; optionally restricting the matching with given start and end parameters.
Python startswith() Syntax
The Python startwith() function follows the below-mentioned syntax:
string.startswith(prefix, start, end)
Note – This function is case-sensitive. If you are looking for ‘p’ then it will only search for instances of a lowercase ‘p’.
startswith() Parameters
Python startswith() accepts 3 parameters:
- prefix – the substring or a tuple of the substring to be checked within the string
- start (Optional) – index position at which the search for the prefix will be started. The default value is 0
- end (Optional) – index position till which the search for the prefix will be ended. The default value is the end of the string
Return value of startswith()
Python startswith() returns the following Boolean values:
- True – if the input string starts with the specified prefix
- False – if the input string does not start with the specified prefix
Example 1: startswith() Without start and end Parameters
Example
# Python program to illustrate startswith()
text = 'Jerry loves to eat cheese'
print('Original string:', text)
result = text.startswith('Jerry')
print('String starts with - Jerry? :', result)
result = text.startswith('eat')
print('String starts with - eat? :', result)
# case-sensitive
result = text.startswith('jerry')
print('String starts with - jerry? :', result)
result = text.startswith('Jerry loves to')
print('String start with - Jerry loves to? :', result)
Output
Original string: Jerry loves to eat cheese
String starts with - Jerry? : True
String starts with - eat? : False
String starts with - jerry? : False
String start with - Jerry loves to? : True
Example 2: startswith() With start and end Parameters
The start and end arguments limit the number of times a prefix in a string is checked as an index. An index begins at 0, so the first character’s index is 0, the second character’s index is 1, and so on. If the end parameter is not specified, it will search until it reaches the end of a string.
Example
# Python program to illustrate startswith()
text = 'This trip is going to be very exciting'
print('Original string:', text)
# specifying start parameter
result = text.startswith('is going', 10)
print('String starts with - is going? :', result)
# specifying start & end parameter
result = text.startswith('going to be', 13, 17)
print('String starts with - going to be? :', result)
result = text.startswith('exciting', 30, 38)
print('String starts with - exciting? :', result)
Output
Original string: This trip is going to be very exciting
String starts with - is going? : True
String starts with - going to be? : False
String starts with - exciting? : True
Passing Tuple to startswith()
Python allows you to supply a tuple of prefixes to the startswith() method. The startswith() returns True if the string begins with any one of the tuple’s items. If it does not, it returns False.
Example 3: startswith() With Tuple Prefix
Example
s = 'Mobile phones are handy'
result = s.startswith(('Laptop', 'Mobile', 'TV', 'Watch'))
# prints True
print(result)
# prints False
result = s.startswith(('Car', 'Bus', 'Ship', 'Cycle'))
print(result)
# with start & end parameter
result = s.startswith(('we', 'they', 'are'), 14, 30)
print(result)
Output
True
False
True
Frequently Asked Questions
Q1. What is startswith in Python?
When working with strings in programming, you may wish to check whether a string begins with a specific value. For example, suppose you’re writing a program that checks to see if a user’s name begins with a specific letter in order to carry out a special lottery event or receive a discount. This is where the Python startswith() function comes into play. Python startswith() is a string method that returns True if the input string starts with the specified prefix(string); else it returns False.
Q2. How do you check if the string starts with a certain prefix in Python?
The Python startswith() string function is used to check whether the input string starts with the specified string; optionally restricting the matching with given start and end parameters. The syntax is as follows:
string.startswith(prefix, start, end)
Example
txt = 'Hello buddy, welcome back'
result = txt.startswith('Hello')
print(result)
result = txt.startswith('welcome', 13)
print(result)
result = txt.startswith('buddy', 7, 15)
print(result)
Output
True
True
False
Q3. Is startswith case sensitive in Python?
The Python startswith() function is case-sensitive. If you are looking for ‘p’ then it will only search for instances of a lowercase ‘p’.
Example
txt = 'Python programming'
result = txt.startswith('Python')
print(result)
result = txt.startswith('python')
print(result)
result = txt.startswith('Programming', 7, 20)
print(result)
Output
True
False
False
Q4. Difference between startswith() and endswith() in Python?
- Python startswith() – The startswith() function determines whether a provided substring/prefix begins with a specific string.
- Python endswith() – The endswith() function determines whether a provided substring/suffix ends with a specific string.
Example
txt = 'Venice is a beautiful travel destination'
# startswith()
result = txt.startswith('Venice')
print(result)
# endswith()
result = txt.endswith('destination')
print(result)
result = txt.endswith('Venice')
print(result)
Output
True
True
False
Leave a Reply