While I was working on a project, I had to clean up messy text data coming from survey forms. Many responses had extra characters at the beginning and end, like brackets, quotes, or even unwanted symbols.
At first, I thought Python would have a direct function for this. But just like in Excel, where you sometimes need a workaround, in Python, you often have multiple ways to solve the same problem. That’s what makes it flexible.
In this tutorial, I’ll show you six easy methods I often use to remove the first and last character from a string in Python. I’ll also share the pros and cons of each method so you can decide which one works best for your situation
Methods to Remove the First and Last Character from a String in Python
Let me explain to you the methods to remove the first and last character from a string in Python.
1: Use .lstrip() and .rstrip() Methods in Python
In Python, the .lstrip method takes a string containing characters as an argument and returns a copy of the string with the specified leading characters removed.
USA_city = 'Chicago'
result = USA_city.lstrip(USA_city[0]).rstrip(USA_city[-1])
print(result)Output:
hicagI executed the above example code and added the screenshot below.

This method removes all leading occurrences of the first character and trailing occurrences of the last character from a string using .lstrip() and .rstrip().
2: Use Python’s removeprefix() and removesuffix() Method
The .removeprefix method in Python is used to check if the string starts with a specified prefix, and if it does, it returns a new string excluding the prefix.
player_name = 'David Backham'
output_name = player_name.removeprefix('D').removesuffix('m')
print(output_name)Output:
avid BackhaI executed the above example code and added the screenshot below.

This method removes a specified prefix and suffix from a string using Python’s .removeprefix() and .removesuffix() methods.
3: Use a for loop in Python
This is another approach to remove the first and last character of a string in Python using a for loop. In Python, a for loop is used to iterate over a sequence of items and collections.
USA_state = "Florida"
print("The input string is:", USA_state)
OutputState = ""
word_length = len(USA_state)
for i in range(1, word_length - 1):
OutputState = OutputState + USA_state[i]
print("The output string is:", OutputState)Output:
The input string is: Florida
The output string is: loridI executed the above example code and added the screenshot below.

This method removes the first and last characters of a string by iterating through it with a for loop and building a new string.
4: Use String Slicing in Python
This method removes the first and last characters of a string in Python using simple string slicing.
text = "Steven Johnson"
print("The input string is:", text)
text_length = len(text)
output_text = text[1:text_length-1]
print("The output string is:", output_text)Output:
The input string is: Steven Johnson
The output string is: teven JohnsoI executed the above example code and added the screenshot below.

String slicing provides a concise and efficient way to extract the desired substring.
5: Use Python’s replace() method
Instead of using the for loop, you can also use the str.replace() method in Python to remove the input string’s first and last character.
input_string = "Phoenix"
new_string = input_string.replace(input_string[0], "").replace(input_string[-1], "")
print(new_string)Output:
hoeniI executed the above example code and added the screenshot below.

This method removes the first and last characters by replacing them with empty strings using str.replace().
6: Use Python Regular Expression
Python provides the built-in re module (Regular Expression) that defines a search pattern. Using this regular expression, we can remove the first and last characters of a string in Python.
import re
Thought = "Do Good And Good Will Come to You"
new_text = re.sub(r'^.|.$', '', Thought)
print(new_text)Output:
o Good And Good Will Come to YoI executed the above example code and added the screenshot below.

This method uses a regular expression with re.sub() to remove the first and last characters of the string efficiently.
I hope the information in the article helps you understand the step-by-step explanation of how to remove the first and last character from a string in Python.
I have demonstrated the six different methods with illustrative examples such as using .lstrip() and rstrip(), using the removeprefix() and removesuffix(), using a for loop, using string slicing, using the replace() method, and using Regular Expression for removing the first and last characters from a Python string.
You may also like to read these articles:
- Append to an Array in Python
- Find the Index of an Element in an Array in Python
- Check if an Array is Empty in Python
- Check the Length of an Array in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.