Skip to main content
added 36 characters in body
Source Link
user1464878
user1464878
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 

Replace \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','') >' Hello World Hi ' 

With regex

s = '''Hello World \t\n\r\tHi There''' regex = re.compile(r'[\n\r\t]') regex.sub("", s) >'Hello World Hi There' 

with Join

s = '''Hello World \t\n\r\tHi There''' ' '.join(s.split()) >'Hello World Hi There' 
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 

Replace \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','') >' Hello World Hi ' 

With regex

regex = re.compile(r'[\n\r\t]') regex.sub("", s) 
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 

Replace \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','') >' Hello World Hi ' 

With regex

s = '''Hello World \t\n\r\tHi There''' regex = re.compile(r'[\n\r\t]') regex.sub("", s) >'Hello World Hi There' 

with Join

s = '''Hello World \t\n\r\tHi There''' ' '.join(s.split()) >'Hello World Hi There' 
added 110 characters in body
Source Link
user1464878
user1464878
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 

Replace \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','') >' Hello World Hi ' 

With regex

regex = re.compile(r'[\n\r\t]') regex.sub("", s) 
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 

Replace \n,\t,\r

s.replace('\n', '').replace('\t','').replace('\r','') >' Hello World Hi ' 

With regex

regex = re.compile(r'[\n\r\t]') regex.sub("", s) 
deleted 25 characters in body; added 1 character in body
Source Link
Georgy
  • 14k
  • 7
  • 69
  • 80
 s = ''''Hello'''Hello World \t\n\r\tHi There''''There'''  # #importimport the module string  import string  # #useuse the method translate to convert  s.translate({ord(c): None for c in string.whitespace}  >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 
 s = ''''Hello World \t\n\r\tHi There''''   #import the module string  import string   #use the method translate to convert  s.translate({ord(c): None for c in string.whitespace}  >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 
s = '''Hello World \t\n\r\tHi There''' # import the module string import string # use the method translate to convert s.translate({ord(c): None for c in string.whitespace} >>'HelloWorldHiThere' 

With regex

s = ''' Hello World \t\n\r\tHi ''' print(re.sub(r"\s+", "", s), sep='') # \s matches all white spaces >HelloWorldHi 
deleted 203 characters in body
Source Link
user1464878
user1464878
Loading
Source Link
user1464878
user1464878
Loading