DEV Community

Murtaja Ziad
Murtaja Ziad

Posted on • Originally published at blog.murtajaziad.xyz on

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

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
Enter fullscreen mode Exit fullscreen mode

[:-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].

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay