In this tutorial, I will explain how to check if a string starts with a specific substring in Python. As a Python developer, I recently faced a real-world issue where I needed to validate user input to ensure it followed a specific format. One of the requirements was to check if the input string started with a particular substring. After researching and experimenting, I discovered several ways to accomplish this task effectively.
String Starts with a Specific Substring in Python
Let us see some important methods to achieve this task.
Method 1. Use the startswith() Method
Python provides a built-in method startswith() that allows you to check if a string starts with a specified substring. This method returns True if the string starts with the given substring otherwise, it returns False. Here’s an example:
name = "John Doe"
if name.startswith("John"):
print("The name starts with 'John'")
else:
print("The name does not start with 'John'")Output:
The name starts with 'John'I have executed the above example code and added the screenshot below.

In this example, we have a variable name containing the string “John Doe”. We use the startswith() method to check if the string starts with the substring “John”. Since it does, the output will be “The name starts with ‘John'”.
Read How to Save an Array to a File in Python?
1. Check Multiple Substrings
Sometimes, you may need to check if a string starts with any of multiple possible substrings. The startswith() method also accepts a tuple of substrings, allowing you to specify multiple options. Here’s an example:
state = "California"
if state.startswith(("New", "North", "South")):
print("The state starts with 'New', 'North', or 'South'")
else:
print("The state does not start with 'New', 'North', or 'South'")Output:
The state does not start with 'New', 'North', or 'South'I have executed the above example code and added the screenshot below.

In this case, we want to check if the state variable starts with either “New”, “North”, or “South”. By passing a tuple of substrings to the startswith() method, we can perform the check efficiently. The output will be “The state does not start with ‘New’, ‘North’, or ‘South'” since “California” does not start with any of those substrings.
Check out How to Use Python Array Index -1 in Python
2. Ignore Case Sensitivity
By default, the startswith() method is case-sensitive, meaning it considers uppercase and lowercase letters as distinct. However, sometimes you may want to perform a case-insensitive check. To achieve this, you can convert both the string and the substring to lowercase (or uppercase) before performing the comparison. Here’s an example:
city = "Chicago"
if city.lower().startswith("chi"):
print("The city starts with 'chi' (case-insensitive)")
else:
print("The city does not start with 'chi' (case-insensitive)")Output:
The city starts with 'chi' (case-insensitive)I have executed the above example code and added the screenshot below.

In this example, we convert the city string to lowercase using the lower() method before applying the startswith() check. By doing so, we can perform a case-insensitive comparison. The output will be “The city starts with ‘chi’ (case-insensitive)” since “Chicago” starts with “chi” regardless of the case.
Read How to Transpose an Array in Python?
Method 2. Use Regular Expressions
Python’s re module provides powerful tools for working with regular expressions. You can use regular expressions to check if a string starts with a specific pattern. Here’s an example:
import re
phone_number = "+1-555-123-4567"
if re.match(r"\+1-\d{3}-\d{3}-\d{4}", phone_number):
print("The phone number starts with the correct format")
else:
print("The phone number does not start with the correct format")In this example, we want to check if the phone_number variable starts with the correct format, which is “+1-” followed by three digits, a dash, three more digits, another dash, and finally four digits. We use the re.match() function to match the regular expression pattern against the phone_number string. The output will be “The phone number starts with the correct format” since “+1-555-123-4567” matches the specified pattern.
Read How to Remove the First Element from an Array in Python?
Method 3. Use any() Function
In some scenarios, you may have a list of possible substrings and want to check if a string starts with any of those substrings. You can achieve this by combining the startswith() method with the any() function. Here’s an example:
fruits = ["apple", "banana", "orange"]
fruit = "apple pie"
if any(fruit.startswith(f) for f in fruits):
print("The fruit starts with one of the elements in the list")
else:
print("The fruit does not start with any element in the list")In this example, we have a list called fruits containing various fruit names. We want to check if the fruit variable starts with any of the elements in the fruits list. We use a list comprehension with the startswith() method to generate a list of boolean values indicating whether the fruit starts with each element in fruits. The any() function then checks if any of those boolean values are True. The output will be “The fruit starts with one of the elements in the list” since “apple pie” starts with “apple”.
Check out How to Count Occurrences in Python Arrays?
Conclusion
In this tutorial, I explained how to check if a string starts with a specific substring in Python. The startswith() method provides an easy way to perform this check. You can also handle multiple substrings by passing a tuple to startswith() , perform case-insensitive comparisons by converting the strings to lowercase or uppercase, checking if a string starts with any element in a list using the any() function, and even use regular expressions for more complex pattern matching.
You may also like to read:
- How to Append to an Array in Python?
- How to Read a File into an Array in Python?
- How to Write an Array to a File in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.