How to remove the last character of a string in Python
In this tutorial, we are going to learn about how to remove the last character of a string in Python.
Removing the last character
To remove the last character of a string, we can use the slice notation
[ ] by passing
:-1 as an argument to it.
Here is an example, that removes the last character
u from the following string:
str = "How are you" print(str[:-1])
"How are yo"
Similarly, we can also use the
rstrip() method by passing the last character as an argument to it.
str = "How are you" print(str.rstrip("u")); # "How are yo"
Another example of
name = "justin" print(name.rstrip("n")); # "justi"