• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Remove Leading and Trailing Characters from String with strip() in Python

Remove Leading and Trailing Characters from String with strip() in Python

June 5, 2022 Leave a Comment

The Python strip() function is very useful when working with strings. strip() removes a given character from the beginning and end of a string.

str = "    1234    "

print(str.strip())

#Output:
1234

When working with strings, the ability to easily be able to manipulate and change the values of those strings is valuable.

One such situation is if you have leading and trailing characters you want to get rid of.

The Python strip() strips a given character from the beginning and end of a string. By default, spaces are removed from the end of the string, but you can pass any character.

Below is an example showing you how to use strip() in Python to remove leading and trailing spaces.

str = "    1234    "

print(str.strip())

#Output:
1234

Removing Trailing Character from String in Python with rstrip() Function

If you have a trailing character and want to get rid of it without doing anything to the beginning of the string, you can use the Python rstrip() function.

rstrip(), or “right strip”, removes a given character from the end of a string if they exist. By default, spaces are removed from the end of the string, but you can pass any character.

Below is an example of how you can remove trailing zeros from a string using rstrip().

str = "12340000"

print(str.rstrip("0"))

#Output:
1234

Removing Leading Character from String in Python with lstrip() Function

If you have a leading character and want to get rid of it without doing anything to the beginning of the string, you can use the Python rstrip() function.

lstrip(), or “left strip”, removes a given character from the beginning of a string if they exist. By default, spaces are removed from the beginning of the string, but you can pass any character.

Below is an example of how you can remove leading zeros from a string using lstrip().

str = "00001234"

print(str.lstrip("0"))

#Output:
1234

Hopefully this article has been useful for you to learn how to the Python strip() function to remove leading and trailing characters in Python.

Other Articles You'll Also Like:

  • 1.  How to Concatenate Tuples in Python
  • 2.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 3.  Convert pandas Series to Dictionary in Python
  • 4.  Check if String Does Not Contain Substring in Python
  • 5.  Convert String into Tuple in Python
  • 6.  Using Python to Read Random Line from File
  • 7.  Using Python to Insert Tab in String
  • 8.  How to Slice a Dictionary in Python
  • 9.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 10.  Using Python to Convert Tuple to String

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 *

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