Kodeclik Logo

Our Programs

Learn More

Schedule

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:

name = "Kodeclik Online Academy."
print(name)

The above program will output:

Kodeclik Online Academy.

Our goal is to remove the last character from this string. There are two natural ways. Lets learn about them!

How to remove the last character from a Python string

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:

name = "Kodeclik Online Academy."
print(len(name))

The output is:

24

This means that the indices of the string run from 0 to 23. We could have printed the same string like so:

name = "Kodeclik Online Academy."
print(len(name))
print(name[0:24])

The output is:

24
Kodeclik Online Academy.

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:

name = "Kodeclik Online Academy."
print(len(name))
print(name[0:23])

Now we obtain:

24
Kodeclik Online Academy

without the period at the end.

The above solution is not so elegant because the number 23 is hardwired. Instead we can just do:

name = "Kodeclik Online Academy."
print(len(name))
print(name[0:len(name)-1])

which will produce the same output:

24
Kodeclik Online Academy

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:

name = "Kodeclik Online Academy."
print(name[:-1])

The output is again:

Kodeclik Online Academy

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:

name = "Kodeclik Online Academy."
print(name.rstrip('.'))

The output will be:

Kodeclik Online Academy

Note that this will only remove this character from the (right) end of the string. Thus, if you do:

name = ".Kodeclik Online Academy."
print(name.rstrip('.'))

you will get:

.Kodeclik Online Academy

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:

name = "Kodeclik Online Academy....."
print(name.rstrip('.'))

The output will be:

Kodeclik Online Academy

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!

name = "Kodeclik Online Academy."
print(name.rstrip(name[-1]))

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:

Kodeclik Online Academy

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.

Kodeclik sidebar newsletter

Join our mailing list

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

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.