Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to replace some character with a tab

I thought it would be straight forward to replace a character by a tab. I try the following.

str1 = "a,b,c,d"
str2 = str1.replace(',','\t')

I expect the str2 to be:

a     b     c     d

Instead I get:

a\tb\tc\td

How to replace a charecter with tab?

like image 811
user3394040 Avatar asked Sep 02 '25 06:09

user3394040


1 Answers

,s are indeed being replaced by \t, but \t is only "interpreted" when str2 is used as with printing to screen or writing to file:

>>>str1 = "a,b,c,d"
>>>str2 = str1.replace(',','\t')

>>>str2
'a\tb\tc\td'

>>>print str2
a       b       c       d
like image 147
That1Guy Avatar answered Sep 04 '25 19:09

That1Guy