Python remove last character from string | Example code

Use the slice method to Python removes the last character from the string in python. Just use the negative index value “-1” to retrieve items from the end of the string.

Negative index starts from -1 to -(iterable_length).

Python remove the last character from the string Example

Simple example code. The index -1 gets you the last element from the iterable. The above program will print the string after removing the last char.

name = 'Python'
res = name[:-1]
print(res)

Output:

Python remove last character from string

Do comment if you have any doubts and suggestions on this Python char string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.