This is probably basic, but I can't find a solution. I have a string, e.g.,
s = 'a,b,c,d'
I'd like to convert the commas to tabs. But using .replace, e.g.,
s.replace[',', '\t']
gives me
>>> s.replace[',', '\t']
'a\tb\tc\td'
I also tried converting to a list and then adding the tabs with a .join, e.g.,
'\t'.join[s.split[',']]
but that gives the same result. How can I have the tabs actually inserted as tabs?
asked Sep 9, 2011 at 23:30
>>> print s.replace[',', '\t']
a b c d
Right now you are see'ing repr[s.replace[',', '\t']]
in the interpreter without the print statement. repr[]
will show each actual character in a string and the interpreter uses that as the default method to print out the result of operations.
Unless you wanted 4 spaces instead; which would just be:
>>> print s.replace[',', [' '*4]]
a b c d
answered Sep 9, 2011 at 23:32
Bài viết liên quan
Cắt chuỗi trong python [slice string python]
Cách sử dụng chuỗi thô raw string trong python
Căn trái, căn giữa và căn phải một chuỗi trong Python [ljust, center, rjust, zfill]
Ký tự đặc biệt trong python [escape sequence]
Phương thức capitalize[] trong Python
Phương thức Center[] trong Python
Chuyển chữ hoa thành chữ thường và ngược lại trong python