startswith() method in Python

The startswith() is a built-in string method checks if a string begins with the specified prefix.
• The prefix can be a string or a tuple of strings.
• Optional arguments start and end let you restrict the search to a slice of the string.

Syntax string.startswith(prefix, start, end)

Example: Basic usage.

Python

filename = "python_programming.txt"
print(filename.startswith("python"))   # True

Example: Using a tuple of prefixes.

Python

languages = ["python", "java", "csharp", "cpp"]
for lang in languages:
    if lang.startswith(("py", "ja")):   # tuple of prefixes
        print(lang)   # prints 'python' and 'java'

Example: With start and end parameters.

Python

text = "python programming"
print(text.startswith("program", 7, 18))   # True