In Python, you can use slicing or the 'startswith' method of the str class to check if a string starts with a specific prefix.
In this example,
def startsWithPrefix(s, prefix):
return s[:len(prefix)] == prefix
str = "Hello World"
prefix = "Hello"
startsWith = startsWithPrefix(str, prefix)
print(f"String starts with prefix '{prefix}': {startsWith}")
String starts with prefix 'Hello': True
In this example,
def startsWithPrefix(s, prefix):
return s.startswith(prefix)
str = "Hello World"
prefix = "Hello"
startsWith = startsWithPrefix(str, prefix)
print(f"String starts with prefix '{prefix}': {startsWith}")
String starts with prefix 'Hello': True
In this tutorial, we learned How to Check if String starts with Specific Prefix in Python language with well detailed examples.