In this article, you will learn about the Python string startswith() method to check a string starts with a specified value or not. It returns True if the string starts with a prefix otherwise False.
Using this function, you can check the various conditions and perform different operations.
In this tutorial, we have seen Python string ljust method to left align the string.
Headings of Contents
Python string startswith() method
Python string startswith method is used to return either True or False. It returns True if the string starts with Prefix otherwise return False.
Syntax
The syntax of the string startswith function in python is:-
string.startswith(prefix, start, stop)
Parameters
The string startswith function in python accepts three parameters.
- prefix:- Required. The value to be check if the string starts with.
- start:- Optional. An integer represents the index number where the starts searching.
- stop:- Optional. An integer represents the index number where the end searching.
Return Value
startswith function in Python returns True or False.
Python string startswith example
Example 1
#use string startswith function with first parameter.
a = 'Programming Funda'
result = a.startswith('P')
print(result)
Output will be:- True
Example 2
#use string startswith function with first and second parameter.
a = 'Programming Funda'
result = a.startswith('F', 12)
print(result)
Output will be:- True
Example 3
#use string startswith function with first,second and third parameters.
a = 'Programming Funda'
result = a.startswith('F', 12, 15)
print(result)
Output will be:- True
Example 4
#What happens, if the string does not start with a specified value.
a = 'Programming Funda'
result = a.startswith('P', 12)
print(result)
Output will be:- False
Example 5
#In this example we will display a message
#if the username starts with V otherwise shows another message.
username = input("Enter you username:- ")
if username.startswith('V'):
print("Your username startswith V.")
else:
print("Your username not starts with V.")
Conclusion
In this tutorial, you have learned Python string startswith method to check a string with a specified value.It returns True if the string start with specified prefix, Then it will return True other return False.
I hope this article will have helped you, if you like this article, please share it with your friends who want to learn Python programming.
Other string methods
- String endswith() method
- String casefold() method
- String center() method
- String capitalize() method
- String count() method
- String index() method
- String format() method
- String isalpha() method
- String isidentifier() method
- String islower() method
- String isupper() method
For More Information:- Click Here