startswith() function checks if a string starts with a specified substring. Python
endswith() checks if a string ends with a substring. Both functions return
Often when you’re working with strings while programming, you may want to check whether a string starts with or ends with a particular value.
For example, if you’re creating a program that collects a user’s phone number, you may want to check if the user has specified their country code. Or perhaps you’re creating a program that checks if a user’s name ends in
e for a special promotion your arcade is doing.
That’s where the built-in functions
endswith() come in.
endswith() can be used to determine whether a string starts with or ends with a specific substring, respectively.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
This tutorial will discuss how to use both the Python
endswith() methods to check if a string begins with or ends with a particular substring. We’ll also work through an example of each of these methods being used in a program.
String Index Refresher
Before talk about startsWith and endsWith, we should take some time to refresh our knowledge on python string index.
A string is a sequence of characters such as numbers, spaces, letters, and symbols. You can access different parts of strings in the same way that you could with lists.
Every character in a string has an index value. The index is a location where the character is in the string. Index numbers start with 0. For example, here’s the string
Python Substrings with index numbers:
The first character in the string is
P with an index value of 0. Our last character,
s, has an index value of 16. Because each character has its own index number, we can manipulate strings based on where each letter is located.
startswith() string method checks whether a string starts with a particular substring. If the string starts with a specified substring, the
startswith() method returns True; otherwise, the function returns False.
Here’s the syntax for the Python
string_name.startswith(substring, start_pos, end_pos)
startswith() with method takes in three parameters, which are as follows:
- substring is the string to be checked within the larger string (required)
- start_pos is the start index position at which the search for the substring should begin (optional)
- end_pos is the index position at which the search for the substring should end (optional)
The substring parameter is case sensitive. So, if you’re looking for
s in a string, it will only look for instances of a lowercase
s. If you want to look for an uppercase
S, you’ll need to specify that character. In addition, remember that index positions in Python start at
, which will affect the
Let’s walk through an example to showcase the
startswith() method in action.
Say that we are an arcade operator and we are running a special promotion. Every customer whose name starts with
J is entitled to 200 extra tickets for every 1000 tickets they win at the arcade. To redeem these tickets, a customer must scan their arcade card at the desk, which runs a program to check the first letter of their name.
Here’s the code we could use to check whether the first letter of a customer’s name is equal to
customer_name = "John Milton" print(customer_name.startswith("J"))
Our code returns: True. On the first line of our code, we define a variable called
customer_name which stores the name of our customer. Then, we use
startswith() to check whether the
“customer_name” variable starts with
J. In this case, our customer’s name does start with
J, so our program returns True.
If you don’t specify the
startswith() will only search for the substring you have specified at the beginning of the string.
Now, let’s say that we are changing our promotion and only people whose name contains an
s between the second and fifth letters of their full name. We could check to see if a customer’s full name contained an
s between the second and fifth letters of their full name using this code:
customer_name = "John Milton" print(customer_name.startswith("s", 1, 5))
Our code returns: False. In our code, we have specified both a
start_pos and an
end_pos parameter, which are set to 1 and 5, respectively. This tells
startswith() only to search for the letter
s between the second and fifth characters in our string (the characters with an index value between 1 and 5).
endswith() string formatting method can be used to check whether a string ends with a particular value.
endswith() works in the same way as the
startswith() method, but instead of searching for a substring at the start of a string, it searches at the end.
Here’s the syntax for the
string_name.endswith(substring, start_pos, end_pos)
The definitions for these parameters are the same as those used with the
Let’s explore an example to showcase how the
endswith() method can be used in Python. Say we are running an airline and we want to process a refund on a customer’s credit card. To do so, we need to know the last four digits of their card number so that we can check it against the one that we have on file.
Here’s an example of
endswith() being used to check whether the four digits given by a customer match those on file:
on_file_credit_card = '4242 4242 4242 4242' matches = on_file_credit_card.endswith('4242') print(matches)
Our program returns: True. In this case, our customer gave us the digits
4242. We used the
endswith() method to verify whether those numbers matched the ones that we had on file. In this case, the credit card on-file ended with
4242, so our program returned True.
We could also use the optional
end_pos arguments to search for a substring at a certain index position.
Say we are operating a cafe and we have a string that stores everything a customer has ordered. Our chef wants to know whether an order contains
Ham Sandwich, and knows the length of our string is 24. The last five characters of our string contain
ORDER. So, we want to skip over the first five characters in our string.
We could perform this search using the following code:
order = "ORDER Latte Ham Sandwich" includes_ham_sandwich = order.endswith("Ham Sandwich", 0, 19) print(includes_ham_sandwich)
Our code returns:
In our example, we specify
Ham Sandwich as our substring parameter.
Then, we specify
start_pos parameter because we are going to be using the
end_pos parameter and
start_pos cannot be blank when we use
end_pos. We specify 19 as our
end_pos argument because the last five characters of our string are
ORDER, and the character before that is a whitespace.
Our string ends in
Ham Sandwich, so our program returns True. If our string did not end in
Ham Sandwich, the suffix otherwise returns False.
Python Endswith with Lists
endswith() can take in a list or a tuple as the
substring argument, which allows you to check whether a string ends with one of multiple strings. Say we are creating a program that checks if a file name ends in either
.mp4. We could perform this check using the following code:
potential_extensions = ['.mp3', '.mp4'] file_name = 'music.mp3' print(file_name.endswith(potential_extensions))
Our code returns: True. In this example, we have created an array called
potential_extensions that stores a list of file extensions. Then, we declared a variable called
file_name which stores the name of the file whose extension we want to check.
Finally, we use the
endswith() method to check if our string ends in any of the extensions in our
potential_extensions list. In this case, our file name ends in
.mp3, which is listed in our
potential_extensions list, so our program returns True.
endswith() methods can be used to check whether a Python string begins with or ends with a particular substring, respectively. Each method includes optional parameters that allow you to specify where the search should begin and end within a string.
This tutorial discussed how to use both the
endswith() methods, and explored a few examples of each method in action. Now you’re ready to check if strings start with or end with a substring using the
endswith() methods like an expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.