
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to check if string or a substring of string starts with substring in Python?
In this article, we are going to find out how to check if a string or a substring of a string starts with a substring in Python.
The first approach is by using the inbuilt method startswith(). This method is used with a string, and we have to give the substring that we want to match as the parameter. Start and end are the two required parameters.
Searching begins from the start index, which is called Start, and ends at the end index, which is called End. It returns True if the given string is started with that substring, otherwise False is returned.
Example 1
In the example given below, we are taking a string and a substring as input and we are checking if the string starts with the substring using the startswith() method −
str1 = "Welcome to Tutorialspoint" substr = "Wel" print("The given string is") print(str1) print("Checking if the given string is starting with",substr) print(str1.startswith(substr))
Output
The output of the above example is given below −
The given string is Welcome to Tutorialspoint Checking if the given string is starting with Wel True
Example 2
In the example given below, we are taking the same program as above but we are taking a different substring and checking −
str1 = "Welcome to Tutorialspoint" substr = "come" print("The given string is") print(str1) print("Checking if the given string is starting with",substr) print(str1.startswith(substr))
Output
The output of the above example is as shown below −
The given string is Welcome to Tutorialspoint Checking if the given string is starting with come False
Using re module
In the second way, regular expressions are employed. To use the re library, import it and install it if it isn't already installed. After importing the re library, we'll utilize Regex, which interprets a prefix as the beginning of a line, so if you're looking for a prefix, this is the way to go.
Example 1
In the example given below, we are taking a string and a substring as input and we are checking if the substring is the start of the string using Regular Expressions −
import re str1 = "Welcome to Tutorialspoint" substr = "Wel" print("The given string is") print(str1) print("Checking if the given string is starting with",substr) print(bool(re.search(substr, str1)))
Output
Following is an output of the above code −
The given string is Welcome to Tutorialspoint Checking if the given string is starting with Wel True
Example 2
In the example given below, we are taking the same program as above but we are taking a different substring and checking −
import re str1 = "Welcome to Tutorialspoint" substr = "come" print("The given string is") print(str1) print("Checking if the given string is starting with",substr) print(bool(re.search(substr, str1)))
Output
The output of the above example is given below −
The given string is Welcome to Tutorialspoint Checking if the given string is starting with come True
- Related Articles
- Python Program to check if a string starts with a substring using regex
- How to check if string or a substring of string ends with suffix in Python?
- Golang Program to check a string starts with a specified substring
- Check if substring present in string in Python
- How to check if a substring is contained in another string in Python
- How to check if a string contains a substring in Golang?
- How to check if the string begins with specific substring in Java?
- How to check if the string ends with specific substring in Java?
- How to check whether a String contains a substring or not?
- Check if a String Contains a Substring in Linux
- PHP to check substring in a string
- Python Program to check if a substring is present in a given string.
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- How to check if an input string contains a specified substring in JSP?
