Remove First and Last Characters from a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# 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'

remove first and last characters from string

The code for this article is available on GitHub

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'

# Only removing the first character from the String

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 = my_str[1:] print(result) # ๐Ÿ‘‰๏ธ 'pple'

only removing first character from string

The code for this article is available on GitHub

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

# Only removing the last character from 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 = my_str[:-1] print(result) # ๐Ÿ‘‰๏ธ 'appl'

only removing last character from string

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.

# Remove first and last characters from a String using strip()

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'

remove first and last characters from string using strip

The code for this article is available on GitHub

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'

# Remove first and last characters from String using removeprefix() and removesuffix()

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'

remove first and last characters using removeprefix and removesuffix

The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev