How do you trim the last character of a string in python?

Python strings come with a number of useful methods and techniques to help you with string manipulation. In this tutorial, we will look at how to remove the last character from a string in Python with the help of some examples.

How to remove the final character of a string?

The simplest way to remove the last character from a string is to slice the string from the first character to the second last character. To slice a string, s from index i to index j [but not including the character at index j], you can use the notation s[i:j].

Strings [and other iterables] in Python are indexed starting from 0. This means that the last character in the string is present at the index len[s] - 1. Let’s now use the two indices to slice the string.

# create a string
s = "Boomerang"
# remove the last character
s[0:len[s]-1]

Output:

'Boomeran'

You can see that the resulting string has the last character from the original string removed. When slicing from the start of the string, you don’t need to explicitly specify the starting index in the slice.

# remove the last character
s[:len[s]-1]

Output:

'Boomeran'

We get the same result as above.

Iterables like strings, lists, etc. in Python work with both positive and negative indexes. The negative index starts from the end of the iterable. For example, item at index -1 is the last item in the iterable, item at the -2 is the second last item and so on.

You can use the -1 index to get the last character in the string.

# create a string
s = "Boomerang"
# the last character
s[-1]

Output:

'g'

We can use this index in the slice to remove the last character from the string.

# create a string
s = "Boomerang"
# remove the last character
s[:-1]

Output:

'Boomeran'

We get the same result as above. Here we slice the string from the start till the last character [but not including it].

There are other methods as well to remove the last character from a string in Python. For example, you can use the string rstrip[] function which by default removes trailing whitespace characters but can be modified to remove any character from the end of a string. Pass the character or the characters to be removed from the end of the string as an argument.

# create a string
s = "Boomerang"
# remove the last character
s.rstrip[s[-1]]

Output:

'Boomeran'

We get the string with the last character removed. Here we pass the last character in s, s[-1] as an argument to the rstrip[] function to indicate that we want to remove this specific character from the end of the string.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    View all posts

Renesh Bedre    1 minute read

In this article, I will discuss how to remove the last number of characters from a string using Python.

As strings are iterable, you can use index and slice methods to access the individual or group of characters within the strings.

Using string slices

In slice, the first character of the string starts with 0 and the last character of the string starts with -1 [equivalent to len[string] - 1].

Remove the last character in a string using a negative index,

x = 'ATGCATTTC'
# # Remove last character
x[:-1]
'ATGCATTT'

# Remove last 2 characters
x[:-2]
'ATGCATT'

Remove the last character in a string using a positive index,

x[:len[x]-1]    # Note: -1 is equivalent len[x]-1
'ATGCATTT'

# Remove last 2 characters
x[:len[x]-2]
'ATGCATT'

Using string rstrip[] function

If you know the last character of a string, you can use the rstrip[] function

You can also remove special characters such as whitespace using rstrip[] function

y = 'ATGCATTTC '
y.rstrip[]
'ATGCATTTC'

Using list

Strings can be converted to a list to remove the last character in a string

''.join[list[x][:-1]]
'ATGCATTT'

Learn more about Python

  • Python enumerate
  • Python tuples
  • 8 different ways to get column names from pandas DataFrame
  • 4 different ways to rename column names in pandas DataFrame

If you have any questions, comments or recommendations, please email me at

This work is licensed under a Creative Commons Attribution 4.0 International License

How do you trim the last character in Python?

Using rstrip[] to remove the last character The rstrip[] is a built-in Python function that returns a String copy with trailing characters removed. For example, we can use the rstrip[] function with negative indexing to remove the final character of the string.

How do you trim the last character of a string?

The easiest way is to use the built-in substring[] method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do you remove the last few characters from a string in Python?

To remove last character from string, slice the string to select characters from start till index position -1 i.e. It selected all characters in the string except the last one.

How do I remove the last 3 characters from a string in Python?

5 Ways to Remove the Last Character From String in Python.
Using Positive index by slicing..
Using Negative Index by Slicing..
Using the rstrip function to Remove Last Character From String in Python..
Using for loop to Remove Last Character From String in Python..
Using regex function..

Bài mới nhất

Chủ Đề