Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether a string starts with XYZ in Python?
A string is a collection of characters stored as a single value. Unlike other technologies, there is no need to explicitly declare strings in Python ? you just need to assign strings to a variable, making Python strings easy to use.
Python provides several methods to check if a string starts with a specific substring. The most common approaches are using the built-in startswith() method and regular expressions.
Using startswith() Method
The startswith() method is the simplest and most efficient way to check if a string begins with a specific substring. It returns True if the string starts with the given prefix, otherwise False.
Syntax
string.startswith(prefix, start, end)
Parameters:
-
prefix? The substring to check for -
start(optional) ? Starting position -
end(optional) ? Ending position
Example 1
Here's how to check if a string starts with "Wel" ?
text = "Welcome to Tutorialspoint"
prefix = "Wel"
print("The given string is:", text)
print("The given substring is:", prefix)
print("Starts with 'Wel':", text.startswith(prefix))
The given string is: Welcome to Tutorialspoint The given substring is: Wel Starts with 'Wel': True
Example 2
Checking if the same string starts with "XYZ" ?
text = "Welcome to Tutorialspoint"
prefix = "XYZ"
print("The given string is:", text)
print("The given substring is:", prefix)
print("Starts with 'XYZ':", text.startswith(prefix))
The given string is: Welcome to Tutorialspoint The given substring is: XYZ Starts with 'XYZ': False
Using Regular Expressions
Regular expressions provide another way to check string patterns. The ^ symbol matches the beginning of a string, making it useful for checking prefixes.
Example 1
Using re.search() with the ^ anchor ?
import re
text = "Welcome to Tutorialspoint"
prefix = "Wel"
print("The given string is:", text)
print("The given substring is:", prefix)
print("Starts with 'Wel':", bool(re.search("^Wel", text)))
The given string is: Welcome to Tutorialspoint The given substring is: Wel Starts with 'Wel': True
Example 2
Testing with "XYZ" prefix ?
import re
text = "Welcome to Tutorialspoint"
prefix = "XYZ"
print("The given string is:", text)
print("The given substring is:", prefix)
print("Starts with 'XYZ':", bool(re.search("^XYZ", text)))
The given string is: Welcome to Tutorialspoint The given substring is: XYZ Starts with 'XYZ': False
Comparison
| Method | Performance | Best For |
|---|---|---|
startswith() |
Faster | Simple prefix checking |
| Regular expressions | Slower | Complex pattern matching |
Conclusion
The startswith() method is the preferred way to check if a string begins with a specific substring due to its simplicity and performance. Use regular expressions only when you need complex pattern matching capabilities.
