October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Remove the First and Last Character from a String in Python

Use Python’s s[1:-1] slice to omit the first and last code points. Here’s how slice bounds, immutability, short inputs, and Unicode affect the result.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Use the slice s[1:-1] to remove the first and last characters from a Python string. It returns everything after the first code point and before the last; the original string is unchanged.

Use a slice to remove both ends

Python slices use the form sequence[start:end]. The start position is included, while the end position is excluded. In s[1:-1], index 1 starts at the second code point, and -1 identifies the final code point as the excluded end boundary.

text = "Python"
trimmed = text[1:-1]

print(trimmed)  # ytho

The expression returns a new string. Python strings are immutable, so slicing does not edit text in place. Assign the result back to the same variable if you want the variable to hold the trimmed string:

text = text[1:-1]

See the Python tutorial’s section on strings for the language’s explanation of string indexing, slicing, and immutability.

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

What happens with short strings?

Slicing handles bounds beyond a string’s length without raising an indexing error. With s[1:-1], an empty string or a string containing one or two code points produces an empty string:

for text in ["", "A", "AB", "ABC"]:
    print(repr(text[1:-1]))

# ''
# ''
# ''
# 'B'

This differs from accessing a specific missing position with ordinary indexing, which can raise an IndexError. If short inputs should be rejected rather than reduced to an empty string, check their length before slicing.

When the ends are known strings

If the goal is to remove a particular prefix or suffix—not simply one code point at either end—use removeprefix() or removesuffix(). These methods remove the specified text only when it matches; they are not substitutes for removing arbitrary first and last positions. See the Python documentation for string methods.

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

Unicode: code points are not always visible characters

Python strings are sequences of Unicode code points. For ordinary text, removing the first and last code points usually matches what people mean by removing the first and last characters. But a visible character can be made of multiple code points—for example, a base letter followed by a combining accent. A basic slice can split such a sequence and leave behind a mark that displays unexpectedly.

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

So s[1:-1] is the right direct operation when the requirement is to remove one code point at each end. If the requirement is to remove complete user-perceived characters (grapheme clusters), basic string slicing alone does not express that boundary; use a grapheme-aware approach appropriate to the application.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.