How to remove the last character of a string in Python?

January 08, 2021

You can remove the last character of a string using Python easily by removing the last index of the string.

string = "abcdef";
print(string[:-1]) # abcde

[:-1] means get the string starting from beginning to the end excluding the last character. Also, you can use it with any index using this syntax: string[start:stop].


Written by Murtaja Ziad, a full-stack web developer.
My website - Follow me on Twitter