Katherine
0
Q:

strip whitespace python

string ='   abc   '

# After removing leading whitespace
print(string.lstrip());
# Output: 'abc   '

# After removing trailing whitespace
print(string.rstrip());
# Output: '   abc'

# After removing all whitespace
print(string.strip());
# Output: 'abc'
8
>>> s.strip()
'Hello  World   From Pankaj \t\n\r\tHi There'
4
s = '  Hello   World     From  Pankaj  \t\n\r\t  Hi       There        '

>>> s.replace(" ", "")
'HelloWorldFromPankaj\t\n\r\tHiThere'
3
sentence = ' hello  apple'
sentence.strip()
>>> 'hello  apple'
6
st = " a "
strip(st)
#Output : "a"
2
a = "      yo!      "
b = a.strip() # this will remove the white spaces that are leading and trailing
2
## Remove the Starting Spaces in Python
 
string1="    This is Test String to strip leading space"
print (string1.lstrip())
3

New to Communities?

Join the community