String processing
You can change (substitute) characters in a string by using the method replace
. For example, suppose we wish to replace all occurrences of '$'
in the string price
with '£'
:
price = 'eggs $2, cheese $4'
price = price.replace('$', '£') # Replace $ by £ in the string price
If we print price
, we now get 'eggs £2,
cheese £4'
.
Other string methods are upper()
(convert text to upper case), lstrip()
(remove leading characters), and rstrip()
(remove trailing characters). Let x ='###this Is A test???'
. Consider the following sequence:
x = x.lstrip('#') # Remove left-hand leading '#' characters to get x = 'this Is A test???'
x = x.rstrip('?') # Remove right-hand trailing '?' characters to get x = 'this...