How to remove the first and last character from a string in Python [6 Ways]

Do you want to learn how to remove the first and last character from a string in Python? Let me show you six different methods in Python to remove the first and last characters from the string with descriptive examples.

To learn all the methods, keep reading this Python article to remove the first and last characters from a string in Python.

  • Using .lstrip() and .rstrip()
  • Using removeprefix() and removesuffix()
  • Using for loop
  • Using string slicing
  • Using the replace method
  • Using regular expression

Remove the first and last character from a string in Python using .lstrip() and .rstrip()

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.

In contrast, the .rstrip method removes the trailing characters from a string.

Here is the complete code to remove the first and last characters from a string in Python using .lstrip() and.rstrip() methods.

USA_city = 'Chicago'

result = USA_city.lstrip(USA_city[0]).rstrip(USA_city[-1])
print(result)

This code line of Python removes all the leading occurrences of the first character and all the trailing occurrences of the last character from the string.

result = USA_city.lstrip(USA_city[0]).rstrip(USA_city[-1])

Output:

hicag

The screenshot below shows the Python program’s output to remove the first and last characters in Python using the .lstrip and .rstrip.

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

Python program to remove the first and last character using the removeprefix() and removesuffix()

The alternate way to remove the first and last characters in a Python string is using the removeprefix() and removesuffix() methods.

READ:  How to Create a String of Same Character in Python

The .removeprefix method in Python is used to check if the string starts with a specified prefix and if it does, returns a new string excluding the prefix.

Otherwise, the .removedsuffix method checks if the string ends with the specified suffix, and if it does, it will return a new string excluding the suffix.

Below is the complete Python program to remove the first and last characters of a string using the .removeprefix() and .removesuffix().

player_name = 'David Backham'

output_name = player_name.removeprefix('D').removesuffix('m')
print(output_name)

Here in this Python code .removeprefix() and removesuffix() methods are used to remove the first and last character of the given player_name.

output_name = player_name.removeprefix('D').removesuffix('m')

Output:

avid Backha

You can refer to the below image to see the output of the code to remove the first and last character from a string in Python.

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

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

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.

This is the complete source code to remove the first and last character from a string in Python using a for loop.

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)

It will first iterate over the range of indices from 1 to word_length-1, excluding the first and last characters of the string. Then, concatenate each character of the string to the OutputState string.

for i in range(1, word_length - 1):
    outputState = outputState + USA_state[i]

Output:

The input string is: Florida
The output string is: lorid

Below is the screenshot that will show the output of the Python program to remove the first and last character of a string in Python.

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

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

Below is the full code of how to remove the first and last characters in Python using 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)

Here, I used a string-slicing notation that specifies the range of indices to extract from the string, as it excludes the first and last characters for the given string.

output_text = text[1:text_length-1]

Output:

The input string is: Steven Johnson
The output string is: teven Johnso

Refer to the image below to see the Python program’s output to remove a string’s first and last character.

Remove first two and last two characters from string Python

How to remove the first and last character from a string in Python using 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.

READ:  Python Array | Python Array Update [With Examples]

This is a source code to remove the first and last characters of a string in Python using the replace() method.

input_string = "Phoenix"
new_string = input_string.replace(input_string[0], "").replace(input_string[-1], "")
print(new_string)

I used the str.replace method that replaces the occurrences of the first and last characters of the input_string with empty strings and effectively removed them.

new_string = input_string.replace(input_string[0], "").replace(input_string[-1], "")

Output:

hoeni

You can refer to the image below to see the output to remove the first and last character of a string in Python using the replace() method.

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

Remove the first and last character of a string in Python using 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 character of a string in Python.

Here is the full source code for Python to remove the first and last characters of a string using Regular Expression in Python.

import re

Thought = "Do Good And Good Will Come to You"
new_text = re.sub(r'^.|.$', '', Thought)
print(new_text)

This line of Python code performs a regular expression substitution to remove the first and last characters from Thought. This ^. matches the first character of the string, and this .$ matches the last character of the string.

new_text = re.sub(r'^.|.$', '', Thought)

Output:

o Good And Good Will Come to Yo

Refer to the screenshot below to see the output of the code:

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

Conclusion

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 seven different methods with illustrative examples such as using .lstrip() and rstrip(), using the removeprefix() and removesuffix(), using a for loop, using string slicing, using replace() method, and using Regular Expression for removing the first and last characters from a Python string.

READ:  Fibonacci series in Python [Complete program with 13 different examples]

You may also like to read these articles: