How to check if a string starts with another in Python
In this tutorial, we are going to learn about how to check if a string starts with another substring in Python.
Python has a built-in startswith()
method by using that we can check if a given string starts with another string or not.
The startswith()
method returns true
if a string starts with a specified prefix; otherwise it returns false
.
Here is an example that checks, if a given string starts with string wel
:
msg = "welcome to a new home"
result = msg.startswith("wel")
print (result)
Output:
True
False case:
msg = "welcome to a new home"
result = msg.startswith("opo")
print (result)
Output:
False