You are currently viewing Python String Startswith

Python startswith() method is indeed used to check whether a given string starts with a specified prefix. It returns True if the string starts with the specified prefix otherwise, it returns False.

Advertisements

In this article, I will explain the Python string startswith() method and using its syntax, parameters, and usage how we can check whether a specified string starts with a given prefix or substring with examples.

1. Quick Examples of String Startswith

If you are in a hurry, below are some quick examples of string startswith() method.


# Quick examples of string startswith

# Initialization string
string = "Welcome To SparkByExamples"

# Example 1: Check if the string starts with "Welcome"
result = string.startswith("Welcome")

# Example 2: Check if the string starts 
# With "SparkByExamples"
result = string.startswith("SparkByExamples")

# Example 3: Check if the substring starting 
# From index 12 starts with "Spark"
result = string.startswith("Spark", 12)

# Example 4: Check if the substring starting 
# From index 11 and ending at index 17 starts with "SparkBy"
result = string.startswith("SparkBy", 11, 18)

# Example 5: Check if the string starts 
# With any of the prefixes in the tuple
result = string.startswith(("Welcome", "To", "SparkByExamples"))

2. Python String startswith() Method

The startswith() method in Python returns True if a string starts with the specified prefix otherwise, it returns False.

2.1 Syntax of String startswith() Method

Following is the syntax of the string startswith() method.


# Syntax of string.startswith() function
string.startswith(prefix, start, end)

2.2 Parameter of String startswith()

  • prefix – The prefix you want to check if the string starts with.
  • start (optional) – The starting index from where the search should begin in the string. If not provided, it starts from the beginning (index 0).
  • end (optional) – The ending index where the search should stop in the string. If not provided, it searches up to the end of the string.

2.3 Return Value

It returns a boolean value.

  • If the string starts with the given prefix it returns True.
  • If the string doesn’t start with a specified prefix it returns False.

3. Use startswith() Without Start and End Parameters

You can use the startswith() method without the start and end parameters to check if a string starts with a specific prefix.

In the below example, the string variable is initialized with the value “Welcome To SparkByExamples“. Apply this method to the string variable to check if it starts with the substring “Welcome“. The result of this check is stored in the result variable. The print() function is used again to display the value of the result variable. This method returns True because the string does indeed start with the prefix “Welcome“.


# Initialization the string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the string starts with "Welcome"
result = string.startswith("Welcome")
print("Check the string starts with specified starts:",result)

Yields below output.

python string startswith

Another example is that you can check if a string starts with the specified prefix. For example, the string variable is initialized with the value “Welcome To SparkByExamples” and then applied the startswith() method over the string variable to check if it starts with the substring “SparkByExamples“. The result of this check is stored in the result variable.

In the below example, this method returns False because the string does not start with the prefix “SparkByExamples“.


# Initialization string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the string starts 
# With "SparkByExamples"
result = string.startswith("SparkByExamples")
print ("Check the string starts with specified starts:",result)

Yields below output.

python string startswith

4. Use startswith() With Start and End Parameters

An alternative way to check if a string starts with a specified prefix is by using startswith()method with the start parameter. In the following program, we are checking if the substring starts from the index 12 (inclusive) using the startswith() method. Here, the substring is "Spark". Its corresponding index is 11 since index starts from 0 in Python. This function returns False. Because the string does not start with the specified prefix “Spark”.


# Initialization string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the substring starting 
# From index 12 starts with "Spark"
result = string.startswith("Spark", 12)
print("Check with start parameter:", result)

# Output:
# Original string: Welcome To SparkByExamples
# Check with start parameter: False

Similarly, you can use the startswith() method with both the start and end parameters. In this program, you’re checking if the substring starting from index 11(inclusive) and ending at index 18(exclusive) starts with "SparkBy". The substring in this range is “SparkBy”, which does match the specified prefix, so the result is True.


# Initialization string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the substring starting 
# From index 11 and ending at index 18(exclusive) starts with "SparkBy"
result = string.startswith("SparkBy", 11, 18)
print("Check with start and end parameters:", result)

# Output
# Original string: Welcome To SparkByExamples
# Check with start and end parameters: True

5. Check if a String Starts with a Substring

To check if the string starts with a specified tuple prefix, use the startswith() method. If the string starts with any of the items in the tuple, the method will return True.

In the below example, apply this method over the string and check whether the string starts with any of the prefixes in the tuple ("Welcome", "To", "SparkByExamples"). Since the string does indeed start with "Welcome", the result is True.


# Initialization string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the string starts 
# With any of the prefixes in the tuple
result = string.startswith(("Welcome", "To", "SparkByExamples"))
print("Check the string starts with specified starts:", result)

# Output:
# Original string: Welcome To SparkByExamples
# Check the string starts with specified starts: True

Conclusion

In this article, I have explained the Python string startswith() method and using its syntax, parameters, and usage how we can check the string starts with a specified prefix. And also explained how it returns a boolean value like True or False when the given string satisfies with a specified prefix.

Happy Learning !!