@Suresh , You can use the .strip() method to remove whitespaces but it has a disadvantage which is it removes other whitespace characters like \t , \n etc , so you need to specify the whitespaces as an argument.
Ex-
‘Hey ‘ .strip()
>>>’Hey’
‘ Hey ‘.strip()
>>>’Hey’
‘ Hey’.strip()
>>>’Hey’
‘harsh has a dog ’.strip()
>>>’harsh has a dog’
‘ Hey \n’.strip(“ “)
>>>’Hey\n’
If you want to remove only one white space from the string use:
def striponeSpace(a):
if a.endswith(" "): a = a[:-1]
if a.startswith(" "):a = a[1:]
return a
>>> striponeSpace(" Hey ")
' Hey'
You can also use myString.rstrip() to remove all the trailing whitespaces from the string and myString.lstrip() to remove all the leading whitespaces from the string