Python String startswith() Method
The Python string startswith() method checks whether a string begins with specific text.
It is useful when you want a simple True or False answer. Beginners often use it to test commands, filenames, labels, and other text patterns.
text = "Python Beginner Help"
print(text.startswith("Python")) # True
print(text.startswith("Beginner")) # False
Use startswith() when you want to check whether a string begins with certain text.
What startswith() does
startswith() is a string method that checks the beginning of a string.
- It checks whether a string begins with a given value
- It returns
TrueorFalse - It does not change the original string
- It works only on strings
Example:
name = "Python"
print(name.startswith("Py")) # True
print(name.startswith("py")) # False
Basic syntax
The main form is:
string.startswith(prefix)
You can also use:
string.startswith(prefix, start)
string.startswith(prefix, start, end)
Here, prefix is the text you want to check at the beginning.
Example:
text = "hello world"
print(text.startswith("hello")) # True
print(text.startswith("world")) # False
print(text.startswith("world", 6)) # True
If you are new to Python strings, see Python strings explained: basics and examples.
Parameters explained
startswith() can take up to three arguments.
prefix
This is the starting text you want to match.
text = "banana"
print(text.startswith("ban")) # True
start
This optional number tells Python where to begin checking.
text = "hello world"
print(text.startswith("world", 6)) # True
end
This optional number tells Python where to stop checking.
text = "hello world"
print(text.startswith("wor", 6, 9)) # True
Multiple prefixes
prefix can also be a tuple of strings. This lets you test more than one possible beginning.
filename = "img_001.png"
print(filename.startswith(("img_", "photo_"))) # True
Return value
startswith() always returns a Boolean value:
Trueif the string starts with the prefixFalseif it does not
This makes it useful in if statements.
command = "/help"
if command.startswith("/"):
print("This looks like a command.")
else:
print("This is normal text.")
Output:
This looks like a command.
Using startswith() with start and end
The start and end arguments let you check part of a string without slicing it yourself.
startlets you begin checking from a later positionendlimits the part of the string being checked- This is useful when working with substrings
Example:
text = "one two three"
print(text.startswith("two", 4)) # True
print(text.startswith("three", 8, 13)) # True
print(text.startswith("three", 8, 12)) # False
In the last example, the end position stops too early, so the full prefix cannot match.
Using multiple prefixes
If more than one prefix is allowed, pass a tuple.
This is cleaner than writing several or conditions.
filename1 = "img_101.jpg"
filename2 = "photo_202.jpg"
filename3 = "doc_303.jpg"
allowed = ("img_", "photo_")
print(filename1.startswith(allowed)) # True
print(filename2.startswith(allowed)) # True
print(filename3.startswith(allowed)) # False
This is especially helpful when checking file naming rules.
You may also want to compare this with the Python string endswith() method if you need to check the end of a string instead.
Case sensitivity
startswith() is case-sensitive.
That means "Python" and "python" are different.
text = "Python"
print(text.startswith("Py")) # True
print(text.startswith("py")) # False
If you want a case-insensitive check, convert the string first with lower().
text = "Python"
print(text.lower().startswith("py")) # True
When to use startswith()
startswith() is useful in many real programs.
Common uses include:
- Checking filename patterns at the start
- Checking user input prefixes such as commands
- Validating text formats like
http://orhttps:// - Making code easier to read than manual slicing
Example:
url = "https://example.com"
if url.startswith(("http://", "https://")):
print("This looks like a web URL.")
Common mistakes
Here are some common beginner mistakes when using startswith().
Using the wrong letter case
startswith() is case-sensitive.
text = "Python"
print(text.startswith("python")) # False
Fix:
print(text.lower().startswith("python")) # True
Calling startswith() on a non-string value
This method works on strings, not numbers or lists.
value = 123
# This will cause an error:
# print(value.startswith("1"))
Fix:
value = str(123)
print(value.startswith("1")) # True
Confusing startswith() with in
startswith() checks only the beginning.
If you want to know whether text appears anywhere, use in or methods like find().
text = "hello world"
print(text.startswith("world")) # False
print("world" in text) # True
For a task-focused guide, see how to check if a string contains a substring in Python.
Forgetting that start and end use index positions
Python counts string positions starting at 0.
text = "abcdef"
print(text.startswith("cd", 2)) # True
Passing a list instead of a tuple
Multiple prefixes must be given as a tuple, not a list.
Wrong:
# text.startswith(["Py", "Ja"])
Correct:
text = "Python"
print(text.startswith(("Py", "Ja"))) # True
Useful debugging checks:
print(text)
print(type(text))
print(text.startswith("Py"))
print(text.lower().startswith("py"))
print(text.startswith(("Py", "Ja")))
FAQ
Does startswith() change the string?
No. It only checks the string and returns True or False.
Can startswith() check more than one prefix?
Yes. Pass a tuple of strings, such as:
text.startswith(("a", "b"))
Is startswith() case-sensitive?
Yes. Uppercase and lowercase letters are treated as different.
What is the difference between startswith() and in?
startswith() checks only the beginning. in checks whether text appears anywhere in the string.
Can I use start and end with startswith()?
Yes. They let you limit which part of the string is checked.