-1

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
handlerFive
  • 870
  • 10
  • 23
imharjyotbagga
  • 199
  • 4
  • 17
  • What do you mean by "not using the `"\t"` tab" ? – Patrick Haugh Aug 27 '18 at 17:38
  • @PatrickHaugh means i dont want to use "\t" (backslash tab) in the print statements, just create a space tab anywhere. I want solutions using loops – imharjyotbagga Aug 27 '18 at 17:40
  • 2
    A [tab](https://en.wikipedia.org/wiki/Tab_key) isn't simply a sequence of spaces. Do you mean that you want to print each char in a string so that it's [left-justified](https://docs.python.org/3/library/stdtypes.html#str.ljust) in a field of a given width? There are several ways to do that efficiently in Python. – PM 2Ring Aug 27 '18 at 17:44
  • I'm unclear as to how you intend to print a tab character without ever using that character in your code. "Using loops" doesn't change the problem, any more than it would for any other character. Can you please clarify? – Prune Aug 27 '18 at 18:11

3 Answers3

3

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.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Thanks, it worked! but please can you explain what does the * operator do. What is its function here? – imharjyotbagga Aug 27 '18 at 17:46
  • @user9969041 Please see https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters and the links on that page. And in the tutorial: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists – PM 2Ring Aug 27 '18 at 17:51
  • @user9969041 This is called argument unpacking. We take the iterable `a` (a string) and unpack it so that each of its elements is a separate argument to `print`. See: https://stackoverflow.com/a/12786141/6779307 – Patrick Haugh Aug 27 '18 at 17:52
1

You can also use the join method:

spacer = '    '
seq = 'Hello'
print(spacer.join(seq))
girlvsdata
  • 1,596
  • 11
  • 21
U3.1415926
  • 812
  • 12
  • 30
0

You can do this by importing sys if you would like to only print as print includes new line for every sentence, but 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]))
Stanley
  • 2,434
  • 18
  • 28