Python String startswith Method - Python Tutorial | Logic Practice
Python Course / Other / Python String startswith Method

Python String startswith Method

BLUF: This lesson on Python String startswith Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String startswith Method

Mastering Python String startswith Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The startswith function in Python yields a Boolean value, either True or False. It evaluates to True if the string commences with the specified prefix; otherwise, it will return False. This method accepts two parameters: 'start' and 'end'. The 'start' parameter indicates the index from which the search begins, while the 'end' parameter denotes the index at which the search concludes.

Signature

Example

startswith(prefix[, start[, end]])

Parameters

prefix : A string which is to be checked.

start : Start index from where searching starts.

end : End index till there searching performs.

Both start and end are optional parameters.

Return

It returns boolean value either True or False.

Let's explore several examples of the startswith method to gain a better understanding of its functionality.

Python String startswith Method Example 1

To begin with, let's develop a straightforward example that outputs True if the specified string begins with the given prefix.

Example

# Python String startswith() method
# Declaring variable
str = "Hello World"
# Calling function
str2 = str.startswith("Hello")
# Displaying result
print (str2)

Output:

Python String startswith Method Example 2

In the event that the string does not commence with the specified prefix, the function will yield a result of False. Refer to the example provided below.

Example

# Python String startswith() method
# Declaring variable
str = "Hello World"
# Calling function
str2 = str.startswith("Python") # False
# Displaying result
print (str2)

Output:

Python String startswith Method Example 3

This approach accepts three arguments. The beginning and ending indices are not mandatory. In this instance, we are providing only the starting index.

Example

# Python String startswith() method
# Declaring variable
str = "Hello World"
# Calling function
str2 = str.startswith("Python",6)
# Displaying result
print (str2)

Output:

Python String startswith Method Example 4

It yields a true value when the string resides between the specified start and end indices, beginning with the given prefix. An illustration is provided to clarify the procedure.

Example

# Python String startswith() method
# Declaring variable
str = "Hello World"
# Calling function
str2 = str.startswith("Python",6,10)
# Displaying result
print (str2)
str2 = str.startswith("Python",8,12)
# Displaying result
print (str2)

Output:

Output

True
False

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience