Remove the First and Last Character from a String in Python

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:

hicag

I executed the above example code and added the screenshot below.

Remove first and last characters of a string using the .lstrip and .rstrip in Python

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 Backha

I executed the above example code and added the screenshot below.

Python program to remove the first and last character of a string

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: lorid

I executed the above example code and added the screenshot below.

How to remove the first and last characters of a string using a for loop in Python

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 Johnso

I executed the above example code and added the screenshot below.

Remove first two and last two characters from string Python

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:

hoeni

I executed the above example code and added the screenshot below.

Python program to remove the first and last character of string using Regular Expression

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 Yo

I executed the above example code and added the screenshot below.

How to remove the first and last character of a string using Regular Expression in Python

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.