Kodeclik Blog
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.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
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!
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:
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.
name = "Kodeclik Online Academy."
print(name)
Kodeclik Online Academy.
name = "Kodeclik Online Academy."
print(len(name))
24
name = "Kodeclik Online Academy."
print(len(name))
print(name[0:24])
24
Kodeclik Online Academy.
name = "Kodeclik Online Academy."
print(len(name))
print(name[0:23])
24
Kodeclik Online Academy
name = "Kodeclik Online Academy."
print(len(name))
print(name[0:len(name)-1])
24
Kodeclik Online Academy
name = "Kodeclik Online Academy."
print(name[:-1])
Kodeclik Online Academy
name = "Kodeclik Online Academy."
print(name.rstrip('.'))
Kodeclik Online Academy
name = ".Kodeclik Online Academy."
print(name.rstrip('.'))
.Kodeclik Online Academy
name = "Kodeclik Online Academy....."
print(name.rstrip('.'))
Kodeclik Online Academy
name = "Kodeclik Online Academy."
print(name.rstrip(name[-1]))
Kodeclik Online Academy