Use Python’s str.startswith() method to check whether a string begins with a specific prefix. The method returns True when the prefix matches the beginning of the string and False otherwise. It can also test multiple prefixes at once and can limit the check to a selected range of the string.

Check if a Python string starts with a specific prefix using startswith()

A Python string is a sequence of characters. When processing URLs, file names, commands, identifiers, or other text, you may need to determine whether a string begins with a particular substring.

For a literal prefix check, use the str.startswith() method. It is clearer than manually slicing the string and does not require a regular expression.

Python str.startswith() syntax and return value

The syntax of startswith() is:

</>
Copy
 str.startswith(prefix[, start[, end]])

prefix is the string to match at the beginning. It can also be a tuple of strings when you want to test several possible prefixes.

start and end are optional index arguments. If start is supplied, Python begins the prefix check at that position. If end is supplied, the check is limited to the portion of the string before that index.

startswith() returns True if the selected part of the string starts with the given prefix. Otherwise, it returns False. The comparison is case-sensitive.

Important: when running the URL examples below, make sure the variable contains only the URL text, such as https://www.tutorialkart.com/, without any surrounding link-formatting characters.

Python startswith() examples for one prefix, multiple prefixes, and string ranges

1. Check if a Python string starts with one specific prefix

In this example, a website URL is checked for the prefix http. A URL beginning with either http:// or https:// also begins with the characters http, so startswith('http') evaluates to True for those plain URL strings.

Python Program

</>
Copy
#the string
website = '[https://www.tutorialkart.com/](https://www.tutorialkart.com/)'

#prefix
prefix = 'http'

#check if string starts with prefix
isValid = website.startswith(prefix)

print(isValid)

Output

True

The same method can be used to compare the beginning of a string with prefixes that should not match.

Python Program

</>
Copy
#the string
website = '[https://www.tutorialkart.com/](https://www.tutorialkart.com/)'

#check if string starts with prefix
print(website.startswith('http'))
print(website.startswith('www'))
print(website.startswith('com'))

Output

True
False
False

2. Check if a Python string starts with any prefix in a tuple

Pass a tuple as the prefix argument when more than one prefix is acceptable. Python returns True as soon as the string starts with any member of the tuple.

A tuple such as ('http', 'www') is useful when the same string can legally begin in more than one way. Pass the tuple directly to startswith(); a list is not accepted as the prefix argument.

Python Program

</>
Copy
#the string
website = '[https://www.tutorialkart.com/](https://www.tutorialkart.com/)'

#prefix
prefix = ('http', 'www')

#check if string starts with prefix
isValid = website.startswith(prefix)

print(isValid)

Output

True

The result is True when at least one of the supplied prefixes matches the beginning of the string.

3. Use start and end indexes with Python startswith()

The optional start and end arguments let you check a prefix within a selected range without first creating a separate substring. The end position is exclusive, just as it is in normal Python slicing.

Python Program

</>
Copy
#the string
website = '[https://www.tutorialkart.com/](https://www.tutorialkart.com/)'

#check if string starts with prefix
#start specified
print(website.startswith('www', 8)) #True
#both start and end specified
print(website.startswith('www', 8, 12)) #True
print(website.startswith('www', 10, 12)) #False

For example, with the plain string https://www.tutorialkart.com/, index 8 points to the first w in www. Therefore, a prefix check for 'www' can begin at that index.

Check whether a Python string starts with a specific character

A single character is also a string in Python, so startswith() can check the first character directly.

</>
Copy
text = "Python"

print(text.startswith("P"))
print(text.startswith("p"))

Output

True
False

The second result is False because startswith() is case-sensitive.

Perform a case-insensitive prefix check in Python

startswith() does not have a separate case-insensitive option. Normalize both the string and the prefix before comparing them. casefold() is a strong choice for case-insensitive text matching.

</>
Copy
text = "Python Tutorial"
prefix = "python"

matches = text.casefold().startswith(prefix.casefold())

print(matches)

Output

True

Check if a Python string starts with any value from a list

If the prefixes are stored in a list, convert the list to a tuple before passing it to startswith().

</>
Copy
filename = "report_final.pdf"
prefixes = ["report_", "invoice_", "summary_"]

print(filename.startswith(tuple(prefixes)))

Output

True

This pattern is concise when all prefixes are literal strings. If each candidate needs a different condition, use any() with a generator expression instead.

</>
Copy
filename = "report_final.pdf"
prefixes = ["report_", "invoice_", "summary_"]

matches = any(filename.startswith(prefix) for prefix in prefixes)

print(matches)

Check if a Python string starts with a number

To check whether the first character is numeric, first make sure the string is not empty, then test the first character with isdigit().

</>
Copy
text = "2026 report"

starts_with_number = bool(text) and text[0].isdigit()

print(starts_with_number)

Output

True

If you specifically mean an ASCII digit from 0 through 9, you can also use a tuple of digit prefixes.

</>
Copy
text = "7 days"

print(text.startswith(tuple("0123456789")))

Use startswith() instead of regex for a literal Python prefix

For a fixed text prefix such as "https://" or "IMG_", startswith() is usually the most direct choice. Regular expressions are more appropriate when the beginning of the string must match a pattern rather than fixed text.

For example, the following regular expression checks whether a string begins with one or more digits:

</>
Copy
import re

text = "123abc"

matches = re.match(r"\d+", text) is not None

print(matches)

Output

True

Python startswith() behavior to remember

  • startswith() checks only the beginning of the string or the beginning of the range selected by start and end.
  • The comparison is case-sensitive unless you normalize the text first.
  • A tuple can contain several accepted prefixes.
  • If your prefixes are in a list, convert the list to a tuple before passing it to startswith().
  • Use a regular expression only when the prefix itself is a pattern.
  • An empty prefix matches the beginning of any string, so "abc".startswith("") returns True.

Summary of checking string prefixes with Python startswith()

Use str.startswith() when you need to check whether a Python string begins with a known prefix. Pass one string for a single prefix, a tuple for several accepted prefixes, and optional start and end indexes when the check should begin within a specific range. For case-insensitive comparisons, normalize both values before calling startswith().

For the official method definition, see the Python documentation for str.startswith(). You can also continue with this Python Tutorial for more string operations.