JBL
0
Q:

removing spaces in a string python

# Python3 code to remove whitespace 
def remove(string): 
    return string.replace(" ", "") 
      
# Driver Program 
string = ' g e e k '
print(remove(string)) 
8
s = '  Hello   World     From  Pankaj  \t\n\r\t  Hi       There        '

>>> s.replace(" ", "")
'HelloWorldFromPankaj\t\n\r\tHiThere'
3
string=' t e s t ' 
print(string.replace(' ',''))
2
sentence = ' hello  apple'
sentence.strip()
>>> 'hello  apple'
6
## Remove the Starting Spaces in Python
 
string1="    This is Test String to strip leading space"
print (string1.lstrip())
3
>>> import re
>>> re.sub(' +', ' ', 'The     quick brown    fox')
'The quick brown fox'
1
sentence = '       hello  apple         '
sentence.strip()
>>> 'hello  apple'
1
>>> s.replace(" ", "")

0

New to Communities?

Join the community