Free tools Windows power users keep installed
One-click scans. No signup required.
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.
#1 Best Overall
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.
Rank #2
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.
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.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →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.
Quick Recap
Best Value
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.




