1

I'm trying to implement a tab space using the '\t' in my code (see code below), but at print time it outputs '\t, char'. Any help would be greatly appreciated.

paranoid_android = "Marvin"
letters = list(paranoid_android)
for char in letters:
    print('\t', char)
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Datalink
  • 71
  • 3
  • 6

2 Answers2

5

You are probably using Python 2.x. Remove the parentheses. print is a statement in Python 2.x, not a function. With parentheses, Python 2.x will think you are printing a tuple.

paranoid_android = "Marvin"
letters = list(paranoid_android)
for char in letters:
    print('\t', char)
for char in letters:
    print '\t', char

Output:

('\t', 'M')
('\t', 'a')
('\t', 'r')
('\t', 'v')
('\t', 'i')
('\t', 'n')
    M
    a
    r
    v
    i
    n

You can also add the following to the top of a Python 2.x script to Python 2.x work like Python 3.x, where print is a function:

from __future__ import print_function
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Thanks Mark. You are absolutely correct. I just realized it only works with Python 3.5.2 and not Python 2.x. – Datalink Jan 08 '17 at 19:54
0

I was trying to use \t with Python version 2.7 and as a result it was outputting as \t as a string. I then proceeded to use a version Python 3.5.2 and it worked properly.

Thanks

Datalink
  • 71
  • 3
  • 6
  • 2
    Instead of writing your own answer repeating what others have suggested, simply accept their answer as the correct one, and leave a comment under it if you wish. – MattDMo Jan 08 '17 at 20:10