Table of Contents
In this tutorial, we will learn about the Python String startswith() method with the help of examples.
The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False.
Example
message = 'Python is fun'
# check if the message starts with Python
print(message.startswith('Python'))
# Output: True
1. Syntax of String startswith()
The syntax of startswith() is:
str.startswith(prefix[, start[, end]])
2. startswith() Parameters
startswith() method takes a maximum of three parameters:
- prefix – String or tuple of strings to be checked
- start (optional) – Beginning position where prefix is to be checked within the string.
- end (optional) – Ending position where prefix is to be checked within the string.
3. startswith() Return Value
startswith() method returns a boolean.
- It returns True if the string starts with the specified prefix.
- It returns False if the string doesn’t start with the specified prefix.
4. Example 1: startswith() Without start and end Parameters
text = "Python is easy to learn."
result = text.startswith('is easy')
# returns False
print(result)
result = text.startswith('Python is ')
# returns True
print(result)
result = text.startswith('Python is easy to learn.')
# returns True
print(result)
Output
False True True
5. Example 2: startswith() With start and end Parameters
text = "Python programming is easy."
# start parameter: 7
# 'programming is easy.' string is searched
result = text.startswith('programming is', 7)
print(result)
# start: 7, end: 18
# 'programming' string is searched
result = text.startswith('programming is', 7, 18)
print(result)
result = text.startswith('program', 7, 18)
print(result)
Output
True False True
6. Passing Tuple to startswith()
It’s possible to pass a tuple of prefixes to the startswith() method in Python.
If the string starts with any item of the tuple, startswith() returns True. If not, it returns False
7. Example 3: startswith() With Tuple Prefix
text = "programming is easy"
result = text.startswith(('python', 'programming'))
# prints True
print(result)
result = text.startswith(('is', 'easy', 'java'))
# prints False
print(result)
# With start and end parameter
# 'is easy' string is checked
result = text.startswith(('programming', 'easy'), 12, 19)
# prints False
print(result)
Output
True False False
If you need to check if a string ends with the specified suffix, you can use endswith() method in Python.
Related posts:
Python Program to Display Calendar
Python Program to Calculate the Area of a Triangle
Python Program to Illustrate Different Set Operations
Python String isalnum()
Python Program to Make a Flattened List from Nested List
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python pass statement
Python all()
Python Program to Display the multiplication Table
Python id()
Python frozenset()
Python iter()
Python exec()
Python String rfind()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program Read a File Line by Line Into a List
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Remove Duplicate Element From a List
Python Program to Get the Last Element of the List
Java InputStream to String
Machine Learning with Python for everyone - Mark E.Fenner
Python Recursion
Python getattr()
Python String rsplit()
Python String isnumeric()
Jackson – Marshall String to JsonNode
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Convert Celsius To Fahrenheit
Python Set clear()
Split a String in Java
Python globals()
Python bytearray()