In python, the string function startswith() is used to check whether a string starts with another string or not. In this tutorial, we’ll look at its syntax and use-cases along with some examples.
Before we proceed, here’s a quick refresher on python strings – Strings are immutable sequences of unicode characters used to handle textual data. Strings implement all common sequence operations (example, iterations, membership tests, etc). For more, check out our guide on strings and other fundamentals of python.
The startswith() function
As stated above,
startswith() is a string function in python to check whether a string starts with the given prefix or not. The following is its syntax:
sample_string.startswith(prefix, start, end)
Here, sample_string is the string you want to check whether it starts with the prefix or not.
- prefix: The prefix string to be checked.
- start (optional): The starting position from where the prefix is to be checked within the string.
- end (optional): The ending position till where the prefix is to be checked within the string.
startswith() function returns:
Trueif the string starts with the given prefix.
Falseif the string doesn’t start with the given prefix.
Example 1: Without using the optional parameters
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# check if string starts with the prefix s = "To code or not to code" # check if s starts with 'To code' print("Starts with 'To code':", s.startswith('To code')) # check if the match is case-sensitive or not print("Starts with 'TO CODE':", s.startswith('TO CODE')) # check if s starts with 'code' print("Starts with 'code':", s.startswith('code'))
Starts with 'To code': True Starts with 'TO CODE': False Starts with 'code': False
In the above example, we check whether the string
s starts with different prefixes or not. First, we check for the prefix
'To code' which returns
True. Then, we check for the same prefix but with all characters as uppercase,
'TO CODE' which returns
False, this shows that the
startswith() function also takes into account the case of the strings. Finally, we check for the prefix
'code' which rightfully returns
Example 2: Specifying the start and end parameters in the python string startswith() function.
# check if string starts with the prefix s = "To code or not to code" print(s.startswith('code')) print(s.startswith('code', 3)) print(s.startswith('code', 3, 7)) print(s.startswith('code', 3, 6))
False True True False
In the above example, we see the results when providing the start and end parameters to the
startswith() string function. To better understand the use of start and end parameters imagine them as the indices to slice a string, like
s[start:end] and then using the prefix to check whether the sliced string starts with it or not.
For more on python string functions, refer to the python docs.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.