Python String startswith() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String startswith() method

The startswith() method in Python returns True if the string starts with the specified value, otherwise it returns False. The startswith() method is not case sensitive.

Syntax of startswith() method
string.startswith(value, start, end)

value:	The substring to search for(Required)
start:	The integer position to start the search. Default is 0.(Optional)
end:	The integer position to end the search. Default is the end of the string.(Optional)
Example 1

Code

str1 = "Pylenin loves Python"

print(str1.startswith("Pylenin"))

Output

True

The string does starts with substring Pylenin.

Example 2 - Check for case sensitivity

Code

str1 = "Pylenin loves Python"

print(str1.startswith("pylenin"))

Output

False
Example 3 - Search for a different string.

Code

str1 = "Pylenin loves Python"

print(str1.startswith("Python"))

Output

False
Example 4 - Check if string s1 starts with substring s2 within a specific position

Code

s1 = 'I like reading Pylenin blogs on Python'
s2 = 'Pylenin'

# Search between position 15 and 30
print(s1.startswith(s2, 15, 30))

Output

True

As you can see, the substring Python occurs at position 15. Hence, the startswith() method returns True when searched between position 15 and 30.

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide