Kodeclik Blog


Removing the last character from a string in Python

Assume you are given a Python string but it contains a period at the end of it, like so:
The above program will output:
Our goal is to remove the last character from this string. There are two natural ways. Lets learn about them!

Method 1: Use string slicing

String slicing is basically a way to index into the string giving a starting index and an ending index.
The above string has 24 characters as evidenced if we use the len() function:
The output is:
This means that the indices of the string run from 0 to 23. We could have printed the same string like so:
The output is:
In the last line of the above program we are using the slicing approach and specifying the start index (0) and one more than the end index (thus, 24). In other words, by specifying [0:24] we are essentially traversing the string from index 0 to 23.
If we desire to skip the last character, we can simply do:
Now we obtain:
without the period at the end.
The above solution is not so elegant because the number 23 is hardwired. Instead we can just do:
which will produce the same output:
An even more elegant approach is to slice but use negative indices (i.e., counting the indices from the end of the string rather than the beginning of the string). A very simple modification to the above program achieves this goal:
The output is again:

Method 2: Use the rstrip() function

The second approach to removing the last character from a Python string uses the built-in function rstrip(). However this function needs an argument specifying exactly what type of characters should be removed (in our case it'll be the “period”). Here is the code for using this function to remove the last character:
The output will be:
Note that this will only remove this character from the (right) end of the string. Thus, if you do:
you will get:
In other words, the period at the beginning of the string is left unchanged. If you have multiple such removable characters at the end of the string, they will all get removed:
The output will be:
The one drawback of this approach is that it requires you to pass the character to be removed as an argument. What if you don’t know the character? Simple, just find and pass it!
Here we are using name[-1] to find the last character and passing it as the argument to rstrip(). The output will be the same as before:
We have thus seen two different ways to remove the last character from a Python string. In both cases we initially hardwired the approach and then showed how to generalize it so it can work for all cases. Which approach is your favorite?
If you liked this post, explore different ways to print a cleaned version of a Python list.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2024. All rights reserved.