str.strip('characters')
'characters' is optional
str = '***Welcome to getechready***'
new_str = str.strip('*')
print(new_str)
Note: You don't have to write the same character multiple times, single time will work. Python keeps on removing the character till the time it finds the same character. It repeats the process on both side.
Output:>>>Welcome to getechready
str = 'Welcome to getechready'
new_str = str.strip('Wel') #the argument is case senstitve
print(new_str)
>>>come to getechready
Mostly strip() method is used for trimming starting and ending spaces from string.
str = ' Welcome to getechready ' #need not be same number of spaces
new_str = str.strip()
print(new_str)
>>>Welcome to getechready
str = 'racecar'
new_str = str.strip('rca')
print(new_str)
>>>e
str = 'mississippi'
new_str = str.strip('ism')
print(new_str)
>>>pp
Note: 'ism' characters matched from the starting i.e. 'mississi' and only 'i' from 'ism' matched from the end.
str = 'getechready'
new_str = str.strip(['get'])
>>>TypeError: strip arg must be None or str
Also read: rstrip() method and lstrip() method