startswith() method in Python String can be used to check whether a given string is starting with a given substring or not.
Like, string “This is Python” is starting with “This” or “T” or “Thi” or “Th” etc.
Syntax
mainstring.startswith(sub)
sub: Substring that you want to check.
When a substring is found in the main string, the method returns True, else it returns False.
s = "This is Python"
print(s.startswith("Th")) // returns True
print(s.startswith("T")) // returns True
print(s.startswith("Thi")) // returns True
print(s.startswith("This")) // returns True
print(s.startswith("This ")) // returns True
print(s.startswith("Thisw ")) // returns False
That’s all here.
