• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove First and Last Character from String Using Python

Remove First and Last Character from String Using Python

February 14, 2022 Leave a Comment

To remove the first and last character from a string in Python, the easiest way is to use slicing.

string = "This is a string variable"

string_without_first_last_char = string[1:-1]

print(string_without_first_last_char)

#Output:
his is a string variabl

When using string variables in Python, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable.

With slicing, we can easily get rid of the first and last character from a string. To do so, we need to select all of the elements between the first and last character.

To keep everything between the first and last character, we should pass ‘1’ as the start position and ‘-1’ as the end position to create our slice.

Below is how we can truncate a string and remove the first and last character from a string in Python.

string = "This is a string variable"

string_without_first_last_char = string[1:-1]

print(string_without_first_last_char)

#Output:
his is a string variabl

How to Remove the First Character from a String in Python

If you just want to remove the first character from a string in Python, we can adjust our example from above.

To remove the first character in a string using slicing, we know that the first character has index ‘0’. So, to get everything except the first character, we start our slice at position ‘1’ and don’t provide an end position to get everything else in the string.

Below is an example of how to get rid of the first character in a string using Python.

string = "This is a string variable"

string_without_first_char = string[1:]

print(string_without_first_char)

#Output:
his is a string variable

How to Remove the Last Character from a String in Python

If you just want to remove the last character from a string in Python, we can adjust our example from above.

You can use slicing to remove the last character from a string in Python in a very similar way. To remove the last character in a string, pass ‘-1’ as the end position.

string = "This is a string variable"

string_without_last_char = string[:-1]

print(string_without_last_char)

#Output:
This is a string variabl

Hopefully this article has been useful for you to learn how to remove the first and last characters from a string in Python.

Other Articles You'll Also Like:

  • 1.  How to Concatenate Tuples in Python
  • 2.  How to Group By Columns and Find Minimum in pandas DataFrame
  • 3.  Read Pickle Files with pandas read_pickle Function
  • 4.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 5.  Change Python Turtle Background Color with screensize() Function
  • 6.  Check if Variable is Float in Python
  • 7.  How to Iterate Through Lines in File with Python
  • 8.  Drop Last n Rows of pandas DataFrame
  • 9.  Initialize Multiple Variables in Python
  • 10.  How to Check If File is Empty with Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy