REMOVE LEADING AND TRAILING CHARACTERS
Python provides the functions strip(), lstrip(), and rstrip() to remove characters in a text string. Listing A.6 displays the contents of Remove1.py that shows you how to search for a string.
Listing A.6 Remove1.py
text = ' leading and trailing white space '
print('text1:','x',text,'y')
text = text.lstrip()
print('text2:','x',text,'y')
text = text.rstrip()
print('text3:','x',text,'y')
Listing A.6 starts by concatenating the letter x and the contents of the variable text, and then printing the result. The second part of Listing A.6 removes the leading white spaces in the string text and then appends the result to the letter x. The third part of Listing A.6 removes the trailing white spaces in the string text (note that the leading white spaces have already been removed) and then appends the result to the letter x.
The output from launching Listing...