• 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
  • VBA
  • 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.  Adjusting Python Turtle Screen Size with screensize() Function
  • 2.  pandas max – Find Maximum Value of Series or DataFrame
  • 3.  Create Empty Tuple in Python
  • 4.  Using Python to Remove First Character from String
  • 5.  Scroll Down Using Selenium in Python
  • 6.  Using Python to Check if Deque is Empty
  • 7.  islower Python – Check if All Letters in String Are Lowercase
  • 8.  How to Check if Character is Uppercase in Python
  • 9.  Find All Pythagorean Triples in a Range using Python
  • 10.  Print Approximately Equal Symbol in 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy