Python String startswith() Method with Examples

In the previous article, we have discussed Python String replace() Method with Examples
String startswith() Method in Python:

If the string begins with the specified value, the startswith() method returns True; otherwise, it returns False.

Syntax:

string.startswith(value, start, end)

Parameters

value: This is Required. The value used to determine whether the string starts with

start: This is optional. It is an integer. The starting point for the search.

end: This is optional. It is an integer. The ending point for the search.

Return Value:

The method startswith() returns a boolean value.

  • If the string begins with the specified value, it returns True.
  • If the string does not begin with the specified value, it returns False.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks"
Given value = "Hello"

Output:

Checking if the given string startswith{ Hello } or not =  False

Example2:

Input:

Given string = "welcome to python programs"
Given value = "t"
start position = 8
end position = 13

Output:

Checking if the given string startswith{ t } or not =  True

String startswith() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the startswith() function for the given string to check if the given string starts with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks"
# Give the value as static input and store it in another variable.
gvn_valu = "hello"
# Pass the given value as an argument to the startswith() function for the
# given string to check if the given string starts with the given value or not.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string startswith{ hello } or not =  True
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the startswith() function for the given string to check if the given string starts with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs"
# Give the value as static input and store it in another variable.
gvn_valu = "t"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 8
# Give the end position as static input and store it in another variable.
gvn_endpositn = 13
# Pass the given value, start and end positions as arguments to the startswith()
# function for the given string to check if the given string starts with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string startswith{ t } or not =  True

Method #2: Using Built-in Functions (User Input)

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the startswith() function for the given string to check if the given string starts with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the startswith() function for the
# given string to check if the given string starts with the given value or not.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = hello this is btechgeeks
Enter some Random String(value) = Hello
Checking if the given string startswith{ Hello } or not = False
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the startswith() function for the given string to check if the given string starts with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the startswith()
# function for the given string to check if the given string starts with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = welcome to python programs
Enter some Random String(value) = to
Enter some random number = 8
Enter some random number = 14
Checking if the given string startswith{ to } or not = True

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.