Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

remove spaces from string python

  •  In python, we have different functions which remove the spaces between the strings.
  • There are three functions that remove the whitespace between the strings, they are:
  • strip(), rstrip(), lstrip()  are the three functions having the same meaning but some differences.
strip(): It removes the whitespace at the beginning and end.

syntax: str.strip()

lstrip(): It removes the Whitespace at beginning of the string.

syntax: str.rstrip()

rstrip(): It removes the whitespace at end of the string.

syntax: str.rstrip()

Example: write a python to demonstrate the remove space function.

  1. txt="    hello"
  2. print(txt)
  3. print(txt.strip())
  4. print(txt.rstrip())
  5. print(txt.lstrip())
Output:         hello    //this is the original output
                  hello         //after removing both side space
                     hello      //no space at  end returns same 
                hello          //remove space at the beginning.




This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

remove spaces from string python

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×