How Can We Help?
In this tutorial, we will explain how to use Python string startswith() method with basic syntax by example for better understanding.
The String startswith() method returns True if the string starts with a given substring between the given start and end index; otherwise, it returns False.
Syntax:
<string_value/string_variable>.startswith(substring_value/substring_variable, start=o, end=n)
Input Parameters:
- string_value / String variable : The string that we want to check whether it’s starts with specific character or not.
- substring_value/substring_variable : The characters or words that we need verify starting with.
- Start : The start index that we need to check the substring value it’s coming at this index or not. this is an optional parameter.
- End : The end index that we need to end the check of substring value whether it’s starting by substring value or variable or not. this is an optional parameter.
Python Check String Starts With
We can check the string value it’s properly starting with particular character or not by simply using the startswith(substring_value) method.
Example (1)
#Print the result of startswith() method
data='welcome to python'
print ('For string variable:', data.startswith())
Output of example (1)
For string variable: True
In the above example, the string variable ‘msg’ starts with ‘we’, and when we checked the start character of it by passing substring ‘we’ as msg.startswith(‘we’) in the print statement, the output shows the result as True.
Example (2)
#Print the result of startswith() method
print('For string value:','parismumbai'.startswith('s',4,9))
Output of example (2)
For string value: True
In the above example, the string value is ‘parismumbai’. When we checked the character ‘s’ starting index in the string, then the result is 4, so when we use the index range is 4 to 9 and check the ‘s’ starting index by ‘parismumbai’.startswith(‘s’,4,9) in the print statement then output shows the result as True.
Example (3)
#Print the result of startswith() method
print('For string value:','parismumbai'.startswith('s',9))
Output of example (3)
For string value: False
In the above example, the string value is ‘parismumbai’. When we checked the character ‘s’ starting index in the string, then the result is 4, so when we use the start index as 9, which checks the starting index of ‘s’ at 9th position by ‘parismumbai’.startswith(‘s’,9) in the print statement, the output shows the result as False.
In this tutorial, you have learned the string startswith() method in Python. What is it? And how to check whether the string starts with a given substring between the given start and end index or not.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Checking Methods:
- isalnum() : It returns True if all the characters of the string are alphanumeric; otherwise, it returns False.
- isdigit() : It returns True if all the characters of the string are digit; otherwise, it returns False.
- isdecimal() : It returns True if all the characters of the string are decimal; otherwise, it returns False.
- isnumeric() : It returns True if all the characters of the string are numeric; otherwise, it returns False.
- isalpha() : It returns True if all the string characters are in lowercase; otherwise, it returns False.
- islower() : It returns False if all the string characters are in an lowercase; otherwise, it returns False.
- isupper() : It returns False if all the string characters are in an uppercase; otherwise, it returns False.
- isspace() : It returns True if all the characters of a string are whitespace; otherwise, it returns False.
- istitle() : It returns True if the string starts with a given substring between the given start and end index; otherwise, it returns False.
- endswith() : It returns True if the string ends with a given substring between start and end index; otherwise, it returns False.
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python tutorials list:
- Python tutorials list : access all python tutorials.