Python startswith

Python startswith method returns Boolean TRUE if the string starts with the specified substring; otherwise, it returns False. The Python startswith Function is handy when we want to check the starting term of user input.

startswith Syntax

The syntax of the Python startswith function is

String_Value.StartsWith(Substring, Starting_Position, Ending_Position)
  • String_Value: A valid String literal.
  • Substring:String you want to search for.
  • Starting_Position (Optional): If you want to specify the starting point (starting index position), then please specify the index value here. If you omit, the Python startswith Function considers Zero as a starting position.
  • Ending_Position (Optional): Specify the endpoint (Ending index position of the search) here. If you omit, the startswith Function considers the highest index number as the end.

Python startswith function Example

The following set of examples help you understand the startswith Function.

# Python StartsWith Method Example

Str1 = 'Learn Python at Tutorial Gateway';
Str2 = Str1.startswith('Learn')
print('First Output of a StartsWith() method is = ', Str2)

# Performing StartsWith() function directly
Str3 = 'Find Tutorial at Tutorial Gateway'.startswith('Find')
print('Second Output of a StartsWith() method is = ', Str3)

# Using First Index while finding the String
Str5 = Str1.startswith('Python', 6)
print('Third Output of a StartsWith() method is = ', Str5)

# Using First & Second Index while finding the String
Str6 = Str1.startswith('Python', 6, len(Str1) -1)
print('Fourth Output of a StartsWith() method is = ', Str6)

# Using First & Second Index while finding Non existing String
Str7 = Str1.startswith('Python', 7, 21)
print('Fifth Output of a StartsWith() method is = ', Str7)
Python StartsWith

The following String function statement checks whether the string Str1 started with the term ‘Learn’ or not using StartsWith function

Str2 = Str1.startswith('Learn')
print('First Output of a StartsWith() method is = ', Str2)

The Python startswith function allows us to use the Start index position. By specifying the starting index position, we can start checking from the middle of a given string.

Str5 = Str1.startswith('Python', 6)
print('Third Output of a StartsWith() method is = ', Str5)

The startswith function also allows using of Starting and ending indices. By specifying both the starting and ending index positions, we can increase the performance. It checks whether the index position 6 is starting with the substring ‘Python ‘ or not.

Str6 = Str1.startswith('Python', 6, len(Str1) -1)
print('Fourth Output of a StartsWith() method is = ', Str6)

If the startswith function in Python does not find the specified string at the starting point, it returns Boolean False. This statement returns False because this Python function starts looking from 7 (it means term ‘Python’ is skipped) and ends at index position 21. As we all know, substring ‘Python ‘ is in position 6.

Str7 = Str1.startswith('Python', 7, 21)
print('Fifth Output of a StartsWith() method is = ', Str7)