Python string method lstrip

Strip a string from left

lstrip allows us to remove characters from the left of a string.

s.lstrip([chars])

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


import string# w w w. ja  v a  2  s. co  m

badSentence = "\t\tThis sentence has problems.   "
print "\nBad:\n" + badSentence
print "\nFixed:\n" + badSentence.lstrip('\t')

string1 = "\t  \n  This is a test string. \t\t \n"

print 'Original string: "%s"\n' % string1
print 'Using strip: "%s"\n' % string1.strip()
print 'Using left strip: "%s"\n' % string1.lstrip()
print 'Using right strip: "%s"\n' % string1.rstrip()

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary