For example,

a = 'My\t\t\tname\t\tis...'

should become

 'My name is...' 

    4 answers 4

    Use the replace () method, which will replace all tabs with spaces:

     a = a.replace("\t", " ") 

    You can read more about this method here .

    And the same can be done with regular expressions:

     import re s = "The \t fox jumped \t over \t the log." s = re.sub("\s+", " ", s) print s 
    • a = a.replace("\t", " ") - 3 spaces in a row will work - slippyk
    • one
      @slippyk is true, but the question did not say whether to remove or not remove duplicate \ t, and it’s not clear in the final line how many spaces the author has. And I wrote in response that it just changes all the tabs for spaces, which indicates an exact quantitative coincidence. I have provided alternatives, and the author of the question will decide for himself what suits him better =) - Lex Hobbit
    • a = a.replace (r "\ t +", "") # 1, or more \ t in a row will become a space. - Tihon

    Very simple and, IMHO, elegant solution:

     ' '.join(a.split()) 
       x = 'My\t\t\tname\t\tis...'.split('\t') ' '.join([i for i in x if i != '']) 
      • I apologize, I fixed it by mistake - vadim vaduxa
       ' '.join(filter(bool, a.split('\t')))