Strip a string from right in Python

Strip a string

rstrip method allows us to remove characters from the end of a string. It has the following syntax.

s.rstrip([chars])

It Removes trailing whitespace from string s (or characters in chars if passed).


import string# from   www  . ja  v a 2  s  .c  o  m

badSentence = "\t\tThis sentence has problems.   "
#Strip trailing spaces
print "Length = " + str(len(badSentence))
print "Without trailing spaces = " + str(len(badSentence.rstrip(' ')))

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary