Remove first and last characters from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Remove first and last characters from a String in Python #

Use string slicing to remove the first and last characters from a string, e.g. result = my_str[1:-1]. The new string will contain a slice of the original string without the first and last characters.

main.py
my_str = 'apple' # ✅ Remove the first and last characters from a string result_1 = my_str[1:-1] print(result_1) # 👉️ 'ppl' # ✅ Remove the first character from a string result_2 = my_str[1:] print(result_2) # 👉️ 'pple' # ✅ Remove the last character from a string result_3 = my_str[:-1] print(result_3) # 👉️ 'appl'

We used string slicing to remove the first and last characters from a string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[1:-1] starts at the character at index 1 and goes up to, but not including the last character in the string.

main.py
my_str = 'apple' result_1 = my_str[1:-1] print(result_1) # 👉️ 'ppl'

If you only need to remove the first character from the string, start at index 1 and go til the end of the string.

main.py
my_str = 'apple' result_2 = my_str[1:] print(result_2) # 👉️ 'pple'

When the stop index is not specified, the slice goes to the end of the string.

If you only need to remove the last character from the string, omit the start index and specify a stop index of -1.

main.py
my_str = 'apple' result_3 = my_str[:-1] print(result_3) # 👉️ 'appl'

When the start index is not specified, the slice starts at index 0.

The slice in the example goes up to, but not including the last character in the string.

Alternatively, you can use the str.lstrip() and str.rstrip() methods to remove characters from the start and end of the string.

main.py
my_str = 'apple' result_1 = my_str.lstrip('a').rstrip('e') print(result_1) # 👉️ 'ppl' result_2 = my_str.lstrip('a') print(result_2) # 👉️ 'pple' result_3 = my_str.rstrip('e') print(result_3) # 👉️ 'appl' # ✅ access string at index to not hard-code the characters result_4 = my_str.lstrip(my_str[0]).rstrip(my_str[-1]) print(result_4) # 👉️ 'ppl'

The str.lstrip method takes a string containing characters as an argument and returns a copy of the string with the specified leading characters removed.

The str.rstrip method takes a string containing characters as an argument and returns a copy of the string with the specified trailing characters removed.

The methods do not change the original string, they return a new string. Strings are immutable in Python.

You can access the string at index 0 and index -1 to not have to hard-code the characters.

main.py
my_str = 'apple' result = my_str.lstrip(my_str[0]).rstrip(my_str[-1]) print(result) # 👉️ 'ppl'
Note that the str.lstrip() and str.rstrip() methods do not necessarily remove a single character.

They remove all occurrences and combinations of the specified character from the start or end of the string.

main.py
my_str = 'aaappleeee' result_1 = my_str.lstrip('a').rstrip('e') print(result_1) # 👉️ 'ppl' result_2 = my_str.lstrip('a') print(result_2) # 👉️ 'ppleeee' result_3 = my_str.rstrip('e') print(result_3) # 👉️ 'aaappl'

Alternatively, you can use the str.removeprefix() and str.removesuffix() methods.

main.py
my_str = 'apple' result_1 = my_str.removeprefix('a').removesuffix('e') print(result_1) # 👉️ 'ppl' result_2 = my_str.removeprefix('a') print(result_2) # 👉️ 'pple' result_3 = my_str.removesuffix('e') print(result_3) # 👉️ 'appl'

The str.removeprefix method checks if the string starts with the specified prefix and if it does, the method returns a new string excluding the prefix, otherwise it returns a copy of the original string.

The str.removesuffix method checks if the string ends with the specified suffix and if it does, the method returns a new string excluding the suffix, otherwise it returns a copy of the original string.

The difference between str.lstrip and str.removeprefix is that the str.lstrip method removes all combinations of the specified characters, whereas the str.removeprefix method removes only the specified prefix.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.