In this Python tutorial, learn to check whether a string starts with, ends with, or both with a specified string. We will learn to check using the regex and built-in methods such as startswith(), endswith(), and array slicing.
text = "HowToDoInJava.com"
start = "How"
end = "com"
re.search(f"^{re.escape(start)}", text) # true
re.search(f"{re.escape(end)}$", text) # true
text.startswith(start) # true
text.endswith(end) # true
text[:len(start)] == start # true
text[-len(end):] == end # true
1. Check if String starts or ends with Substring using Regular Expressions
In Python, the re.search() is used for pattern matching with regular expressions. It matches a given substring expression against the specified string and returns a Match if the substring is present in the input text, None otherwise.
- To match the substring at the start, use the caret (
^
) sign in the beginning of the expression. - To match the substring at the end, use the dollar (
$
) sign at the end of the expression.
import re
# Input String
text = "HowToDoInJava.com"
# Specified substrings to check
start = "How"
end = "com"
# Using regex to check if the string starts with the specified substring
if re.search(f"^{re.escape(start)}", text):
print("String starts with the specified substring.")
else:
print("String does not start with the specified substring.")
# Using regex to check if the string ends with the specified substring
if re.search(f"{re.escape(end)}$", text):
print("String ends with the specified substring.")
else:
print("String does not end with the specified substring.")
The program output:
String starts with the specified substring.
String ends with the specified substring.
2. Check if String starts or ends with using Built-in Methods
Another straightforward method to determine the start and end of a string is using the startswith() and endswith() methods.
startswith()
: return True if S starts with the specified prefix, False otherwise.endswith()
: return True if S starts with the specified suffix, False otherwise.
# Input String
text = "HowToDoInJava.com"
# Specified substrings to check
start = "How"
end = "com"
# Check if the string starts with the specified substring
if text.startswith(start):
print("String starts with the specified substring.")
else:
print("String does not start with the specified substring.")
# Check if the string ends with the specified substring
if text.endswith(end):
print("String ends with the specified substring.")
else:
print("String does not end with the specified substring.")
Program output:
String starts with the specified substring.
String ends with the specified substring.
3. Check if String starts or ends with using Array Slicing
A string can be seen as an array of characters. We can take advantage of this fact, and convert the string to array, and slice the array from the beginning and the end to verify that the string is starting and ending with the specified substrings.
text[:len(start)]
: expression extracts a substring from the beginning of thetext
string up to the length of the ‘start‘ string. And then we match it with the start substring itself.text[-len(end):]
expression extracts a substring from the end of thetext
string up to the length of the ‘end‘ string. And then we match it with the end substring itself.
# Input String
text = "HowToDoInJava.com"
# Specified substrings to check
start = "How"
end = "com"
# Check if the string starts with the specified substring using slicing
if text[:len(start)] == start:
print("String starts with the specified substring.")
else:
print("String does not start with the specified substring.")
if text[-len(end):] == end:
print("String ends with the specified substring.")
else:
print("String does not end with the specified substring.")
Program output:
String starts with the specified substring.
String ends with the specified substring.
4. Conclusion
This Python tutorial discussed 3 effective ways to check if a given string begins with or ends with the specified substrings. We discussed the solutions using regex, build-in method, and array slicing.
Happy Learning !!
Comments