Remove Leading or Trailing Whitespace in a Python String

17 04 2009

If your using Python and you’ve got a string that you want to remove leading or trailing spaces from then Python makes this very simple. This is how:

myString.lstrip() # This removes leading spaces

myString.rstrip() # This removes trailing spaces

myString.strip() # This removes both leading and trailing spaces.

Additionally you can specify a parameter to the strip commands so if you wanted to strip a comma you can write this:

myString.rstrip(',') # This removes trailing commas


Little Nibblers

About these ads



One response

3 11 2010
Python: How do I remove trailing null spaces from a string? - Quora

[…] trailing null spaces from a string?Cannot add comment at this time.   Niche Nerd…Insert a dynamic date here BIU     @     @ […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.