strip() function in python code example

Example 1: strip in python

txt = "     banana     "
x = txt.strip()
#x will be "banana"

Example 2: python strip

txt = "  test    "

txt.strip()
#Output: "test"

txt.lstrip()
#Output: "test    "

txt.rstrip()
#Output: "  test"

Example 3: python strip

# removes outside whitespace/characters
'  hey  '.strip()     # "hey"
'  hey  '.lstrip()    # "hey  "
'  hey  '.rstrip()    # "  hey"
'_.hey__'.strip('._') # "hey"

Example 4: .strip() python

string = '  xoxo love xoxo   '

# Leading and trailing whitespaces are removed
print(string.strip())

# All <whitespace>,x,o,e characters in the left
# and right of string are removed
print(string.strip(' xoe'))

# Argument doesn't contain space
# No characters are removed.
print(string.strip('stx'))

string = 'android is awesome'
print(string.strip('an'))

Example 5: python strip()

a = "  smurf   "

a = a.strip()
#remove space at begining and end of string
print(a)
#smurf