Python String startswith() Method

Python string startswith() method “returns True if a string starts with the specified prefix(string). If not, it returns False.”

Syntax

str.startswith(prefix, beg=0,end=len(string));

Parameters

  1. prefix − This is the string to be checked.
  2. beg − This is the optional parameter to set a start index of the matching boundary.
  3. end − This is the optional parameter to the end start index of the matching limit.

Return value

The startswith() method returns a boolean.

  1. It returns True if the string starts with the specified prefix.
  2. It returns False if the string doesn’t start with the specified prefix.

Example 1: How to Use startswith() Method

 strA = 'Hello AppDividend'

print(strA.startswith('App', 6))

Output

Python String Startswith Example

Example 2: startswith() With start and end Parameters

strA = 'Hello AppDividend'

print(strA.startswith('app', 6, 10))

Output

Python startswith() Tutorial

Example 3: startswith() With Tuple Prefix

text = "Homer Simpson is funniest characters"

result = text.startswith(('Homer', 'characters'))
print(result)

result_2 = text.startswith(('Bart', 'is', 'funny', 'too'))
print(result_2)

result_3 = text.startswith(('homer', 'characters'), 10, 19)
print(result_3)

Output

True
False
False

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.