How print tabs (long spaces) between 2 characters of a string, in Python using loops and not the "\t" tab? (or in loops, if possible") For Example :
a="HELLO"
so the output must be
H E L L O
How print tabs (long spaces) between 2 characters of a string, in Python using loops and not the "\t" tab? (or in loops, if possible") For Example :
a="HELLO"
so the output must be
H E L L O
You can unpack the string into it's characters and use tabs as a separator in the call to print
a="HELLO"
print(*a, sep="\t")
# H E L L O
If you want to handle the string with the tab-separated letters, you can instead use str.join
tab_separated = '\t'.join(a)
A looping solution would be very inefficient, but would look something like
def tab_sep(s):
if not s:
return s
res = s[0]
for letter in s[1:]:
res += '\t' + letter
return res
You can replace the '\t'
strings in the above with chr(9)
(tab has the ASCII value 9) if you really don't want the escape sequences, but I wouldn't recommend it. It makes it difficult to tell what the code is doing.
You can also use the join
method:
spacer = ' '
seq = 'Hello'
print(spacer.join(seq))
You can do this by importing sys if you would like to only print as
sys.stdout.write
doesnt
a="HELLO"
import sys
for num in range(len(a)):
# print('\t %s' %(a[num]))
sys.stdout.write('\t %s' %(a[num]))