• 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 / Truncate String in Python with Slicing

Truncate String in Python with Slicing

March 1, 2022 Leave a Comment

In Python, the easiest way to truncate a string is with slicing. With slicing, you can truncate strings by any number of characters.

string = "This is a string variable"

string_without_last_three_chars = string[:-3]

print(string_without_last_three_chars)

#Output:
This is a string varia

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 truncate strings and remove characters.

We can easily truncate strings in Python with slicing.

To use slicing, you can pass two indices which correspond to the starting position of the slice and ending position of a slice.

For example, if you wanted to truncate a string and remove the last three characters, you can do so with the following Python code.

string = "This is a string variable"

string_without_last_three_chars = string[:-3]

print(string_without_last_three_chars)

#Output:
This is a string varia

How to Remove the First and Last Character from a String in Python

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

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 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 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 truncate strings in Python with slicing.

Other Articles You'll Also Like:

  • 1.  Convert List to Set with Python
  • 2.  Using Python to Split String by Tab
  • 3.  How to Check if Number is Divisible by 3 in Python
  • 4.  Find Least Common Multiple of Numbers Using Python
  • 5.  Check if File Exists in AWS S3 Bucket Using Python
  • 6.  Get pandas Index Values as List in Python
  • 7.  Flatten List of Tuples in Python
  • 8.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 9.  Get Days in Month from Date in pandas DataFrame
  • 10.  How to Check if Set is Empty 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

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