Last updated: Apr 9, 2024
Reading timeยท3 min
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.
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]
.
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.
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.
my_str = 'apple' result = my_str[1:] print(result) # ๐๏ธ '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
.
my_str = 'apple' result = my_str[:-1] print(result) # ๐๏ธ '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.
strip()
Alternatively, you can use the str.lstrip()
and
str.rstrip() methods to remove
characters from the start and end of the string.
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.
my_str = 'apple' result = my_str.lstrip(my_str[0]).rstrip(my_str[-1]) print(result) # ๐๏ธ 'ppl'
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: