Python String startswith() Method
The str.startswith()
method returns True
if the string starts with the specified prefix; otherwise, it returns False
. The prefix can also be a tuple where multiple prefixes can be looked at.
Example:
text = "Hello, World!"
if (text.startswith("Hello")):
print("The string starts with 'Hello'")
else:
print("The string does not start with 'Hello'")
Output
The string starts with 'Hello'
Syntax
The Syntax of the str.startswith()
function is:
str.startswith(prefix, start=0, end=len(str))
Here, str
is a string.
Parameters
The str.startswith()
function can take three parameters.
- prefix - string or tuple of prefixes that needs to be checked in a string
- start (optional) - The starting index of the string where the search should begin. The default is 0 if the argument is not passed.
- end (optional) - The ending index of the string where the search should end. The default is the end of the string, i.e.,
len(str)
if the argument is not passed.
Return Value
The str.startswith()
function returns True
if the string starts with the provided prefix; otherwise, it returns False
.
Example 1: Check if a string starts with a specific string
The startswith()
method takes in a prefix string as its required argument and optional start and end arguments and returns True
if the string starts with the provided prefix; otherwise, it returns False
.
Here are some examples to illustrate this:
text = 'Hello, World!'
# Check if the string starts with 'Hello,'
print(text.startswith('Hello,'))
# Check if the string starts with 'hello,' (case-sensitive)
print(text.startswith('hello,'))
# Check if the string starts with 'ello,' (starts the search from index 1)
print(text.startswith('ello,', 1))
# Check if the string starts with 'lo,' (starts the search from index 3)
print(text.startswith('lo,', 3))
# Check if the string starts with 'ello,' (starts the search from index 1 and starts at index 7)
print(text.startswith('ello,', 1, 7))
# Check if the string starts with 'Wo' (starts the search at index 2, which is before the prefix)
print(text.startswith('He', 2, 7))
# Check if the string starts with 'W' (starts the search from index 3, which is after the prefix )
print(text.startswith('W', 3))
Output
True
False
True
True
True
False
False
Example 2: Validating an email address to ensure that it starts with a specific prefix
In this example, the is_valid_email()
function checks if the email argument starts with the "mailto:" string. If it does, the function returns True
; otherwise, it returns False
.
def is_valid_email(email):
# check if the email address starts with "mailto:"
if email.startswith("mailto:"):
return True
else:
return False
email = "mailto:john@example.com"
# check if the email address is valid
if is_valid_email(email):
print("Valid email address")
else:
print("Invalid email address")
Output
Valid email address
Example 3: Searching a list of strings for elements that start with a specific prefix
In this example, the code defines a list of IFSC Codes strings in the variable ifsc_codes
and a string called prefix
. It then uses a list comprehension to iterate over the strings list and create a new list called results that contains only the elements from strings that start with the prefix
string.
ifsc_codes = ["ICIC0000001", "ICIC0000002", "SBIN0000001", "SBIN0000002", "SBIN0000003", "ICIC0000004","ICIC0000006"]
prefix = "ICIC"
# use a list comprehension to create a new list of strings that start with the prefix
results = [codes for codes in ifsc_codes if codes.startswith(prefix)]
print(results)
Output
['ICIC0000001', 'ICIC0000002', 'ICIC0000004', 'ICIC0000006']
Reference: Python Official Docs