Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To remove the last character from a Python string, slice up to the final index: text[:-1]. The expression returns a new string, so the original is unchanged unless you assign the result back to a variable. If the string is empty, the slice returns an empty string instead of raising an error.

The one-line answer

Use a slice that ends at -1. A negative index counts from the end of the string, so -1 is the last element, and a slice stops before the index it names. Together, [:-1] means “everything except the last element.”

text = "hello"
shortened = text[:-1]
print(shortened)   # hell
print(text)        # hello (unchanged; the slice made a new string)

text = text[:-1]   # assign back if you want the variable to change
print(text)        # hell

Python’s documentation for the tutorial introduction explains negative indexing and slice boundaries in the same terms, and it is the best place to confirm the mechanics: Python tutorial introduction (CPython repository).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the operation that matches your intent

“Remove the last character” can mean three different things. Pick the expression based on whether the removal should happen unconditionally, only when a particular ending is present, or only for whitespace.

Goal Expression Example result
Remove exactly the last character, whatever it is text[:-1] "hello" becomes "hell"; "" stays ""
Remove one known suffix only if it is present text.removesuffix(".txt") "report.txt" becomes "report"; "report.csv" stays "report.csv"
Remove the last character only when it equals a given value if text.endswith("!"): text = text[:-1] "wow!" becomes "wow"; "wow" stays "wow"
Remove trailing whitespace text.rstrip() "done n" becomes "done"

Use removesuffix when the ending is known

str.removesuffix() was added in Python 3.9 by PEP 616, “String methods to remove prefixes and suffixes”. It checks whether the string ends with the suffix you pass and removes that match. If the suffix is absent, the string comes back unchanged, which makes it safer than slicing by a fixed length when the input is not guaranteed to end the way you expect.

"report.txt".removesuffix(".txt")   # "report"
"report.csv".removesuffix(".txt")   # "report.csv"
"report.txt".removesuffix("")       # "report.txt" (an empty suffix changes nothing)

PEP 616 deliberately avoids implementing the removal as self[:-len(suffix)] when the suffix is empty, because self[:-0] evaluates to an empty string. The empty-suffix case is handled explicitly so it leaves the string intact.

Why rstrip with a string argument surprises people

A common mistake is to write text.rstrip(".txt") to remove a file extension. The argument to rstrip() is not a suffix. It is a set of characters, and the method removes any of those characters repeatedly from the end until it meets one that is not in the set.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
"hello".rstrip("lo")    # "he"  (strips o, l, l, then stops at e)
"hello".rstrip("o")     # "hell"

PEP 616 describes this confusion as a recurring source of questions from users, which is part of why the exact-suffix method was added: PEP 616. If you want exact matching, use removesuffix().

Handle empty strings and non-string values

Slicing an empty string is safe. ""[:-1] returns "" and does not raise an error. The safety does not extend to other types: passing None to the same expression raises TypeError: 'NoneType' object is not subscriptable. Confirm that the variable holds a string before slicing if it can come from optional input.

Python versions earlier than 3.9

If your code must run on a release before 3.9, removesuffix() is not available. Write the check yourself, and keep the empty-suffix guard so that a zero-length suffix is not sliced:

def remove_suffix(text, suffix):
    if suffix and text.endswith(suffix):
        return text[:-len(suffix)]
    return text

The if suffix check matters. Without it, an empty suffix produces text[:-0], which is text[:0], and returns an empty string.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What counts as “one character”

text[:-1] removes one code point, which is the unit Python strings index by. Some characters the eye reads as a single letter or symbol are built from several code points, so removing one code point can leave a partial character behind.

s = "café"    # "café" written as e plus a combining acute accent
print(s[:-1])       # "cafe" (the accent is gone; the e remains)

t = "U0001F44DU0001F3FD"   # thumbs-up plus a skin-tone modifier
print(t[:-1])                # the thumbs-up without the modifier

The built-in slice does not segment text into user-perceived characters. If you must delete a whole visible character, you need a grapheme-aware approach. Third-party packages such as regex support extended grapheme cluster matching with X, but the standard library slice does not.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decision checklist

  • Always remove the final element, and the string is never empty? Use text[:-1].
  • Remove a specific ending only when it is present? Use removesuffix() on Python 3.9 or later.
  • Remove trailing spaces or newlines? Use rstrip() with no argument.
  • Need the character itself rather than the shortened string? Read text[-1].
  • Working with characters that may be multi-part, such as accented letters or emoji with modifiers? Plan for grapheme-aware handling.

For the full definitions of these string methods and slice semantics, see the Python built-in types reference.

Frequently Asked Questions

How do I remove the last N characters from a string?

Use text[:-n], where n is the number of characters to drop. If n is larger than the string’s length, the slice returns an empty string rather than raising an error.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I get the last character without removing it?

Index with text[-1]. This returns the final character as a new one-character string and leaves text unchanged. An empty string raises IndexError with this form, so check the length first.

How do I remove one trailing newline but keep any others?

Use text.removesuffix("n") on Python 3.9 or later. Unlike rstrip("n"), it removes only one newline at the end, not every trailing newline.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.