Python String startswith() Method – Tutorial with Examples

Python String startswith() Method

The startswith() method is a built-in method in Python that returns a boolean value indicating whether a given string starts with a specified substring. It is a useful method for searching for specific patterns in text data.

Syntax

The syntax of the startswith() method is as follows:

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

Here, string is the string that we want to check, prefix is the substring we want to check for at the start of the string, start is the index at which to start the search (optional), and end is the index at which to end the search (optional).

Return Value

The startswith() method returns a boolean value of True if the given string starts with the specified substring, and False otherwise.

Examples

Here are three different examples of how to use the startswith() method in Python:

Example 1: Checking if a String Starts with a Substring

The following example demonstrates how to use the startswith() method to check if a given string starts with a specified substring:

string = "Hello, World!"
result = string.startswith("Hello")
print(result)

Output:

True

In this example, we define a string and use the startswith() method to check if it starts with the substring “Hello”. The resulting boolean value is True, indicating that the string does start with the specified substring.

Example 2: Using Optional Parameters

The following example demonstrates how to use the optional start and end parameters of the startswith() method:

string = "Hello, World!"
result = string.startswith("World", 7)
print(result)

Output:

True

In this example, we define a string and use the startswith() method to check if it starts with the substring “World”, starting the search at index 7. The resulting boolean value is True, indicating that the string does start with the specified substring at the specified index.

Example 3: Checking Multiple Substrings

The following example demonstrates how to use the startswith() method to check if a given string starts with one of several specified substrings:

string = "Hello, World!"
result = string.startswith(("Hello", "Hi", "Hey"))
print(result)

Output:

True

In this example, we define a string and use the startswith() method to check if it starts with any of the specified substrings (“Hello”, “Hi”, or “Hey”). The resulting boolean value is True, indicating that the string does start with one of the specified substrings.

Use Cases

The startswith() method can be used in a variety of situations where you need to check whether a given string starts with a particular substring. Here are some use cases for the startswith() method:

  • Search for file names that start with a certain prefix.
  • Check if a URL begins with “http://” or “https://”.
  • Determine if a sentence starts with a specific word or phrase.
  • Validate input strings by checking if they match a specific format at the beginning.

The startswith() method can be a helpful tool for anyone working with text data and needing to search for specific patterns at the beginning of strings.

Leave a Reply

Your email address will not be published. Required fields are marked *