Python-Ref > String manipulation > String basics > Stripping unwanted characters
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Stripping unwanted characters

How to get rid of unwanted trailing characters.
In the same manner that trailing whitespace characters are removed (Playing with whitespaces) other characters might be removed.
Expand/Shrink
  1   text1 = '"Text in quotes."'
  2   text2 = "** silly trailing characters ^^"
  3   text3 = "(brackets)"
  4   
  5   print text1
  6   print text1.strip('"')
  7   print text1.rstrip('"')
  8   print text1.strip('".')
  9   print
 10   
 11   print text2
 12   print text2.strip("*^")
 13   print text2.strip("*^ ")
 14   print
 15   
 16   print text3
 17   print text3.lstrip("(")
 18   print text3.strip("()")
 19   print text3.strip("*")
stdout:
"Text in quotes."
Text in quotes.
"Text in quotes.
Text in quotes

** silly trailing characters ^^
 silly trailing characters 
silly trailing characters

(brackets)
brackets)
brackets
(brackets)
Doba běhu: 13.9 ms