1

I want to print out columns of text and I am trying to do this through for loops. I tried using this:

a = 0
while a<58:
    for x in Char[a],Numb[a]:
        print (x)
    a+=2

but this prints out the two values on separate lines. Is there any way to print it so it comes out like:

A  1
B  2
C  3
D  4
E  5   ...etc

4 Answers 4

1
a = 0
while a<58:
    print Char[a], Numb[a]
    a+=2

or if you want, you can add some formatting to make it fixed-width:

a = 0
while a<58:
    print '{0:3} {1}'.format(Char[a], Numb[a])
    a+=2

and maybe a little cleaner:

for a in xrange(0,58,2):
    print '{0:3} {1}'.format(Char[a], Numb[a])
Sign up to request clarification or add additional context in comments.

Comments

1

You can use enumerate and a for loop :

>>> import string
>>> for i,j in enumerate(string.ascii_uppercase,1) :
...    print j,'\t',i #in python 3 print (j,'\t',i)
... 
A   1
B   2
C   3
D   4
E   5
F   6
G   7
H   8
I   9
J   10
K   11
L   12
M   13
N   14
O   15
P   16
Q   17
R   18
S   19
T   20
U   21
V   22
W   23
X   24
Y   25
Z   26

Comments

1

Some remarks:

  1. Rewriting the while loop as a for loop is nicer and makes it less likely to get an infinite loop.
  2. zip
  3. Unpacking tuples / lists is nice (e.g. a, b = (1, 2))
  4. Take a look at string formatting with Python, especially the columns section. Or simply use \t (the ASCII-symbol for tab).

As code, it looks like this:

import string

chars = string.ascii_uppercase
digits = string.digits

for a in range(0, min(len(chars), len(digits)), 2):
    for char, digit in zip(chars[a], digits[a]):
        print("{0:>3} {1:>3}".format(char, digit))

Formatting options like {i:>n} mean:

  • >: Right align (you don't need it. You can also take < or nothing.
  • i: i-th element of the tuple
  • n: n columns in total. This is also not necessary.

I usually make something like:

import string

chars = string.ascii_uppercase
digits = string.digits

headers = ["chars", "digits"]
sizes = [(len(el) + 2) for el in headers]
formatter = "{0:>" + str(sizes[0]) + "} {1:>" + str(sizes[1]) + "}"

print(formatter.format(*headers))
print("-"*(sum(sizes) + len(sizes)))
for a in range(0, min(len(chars), len(digits)), 2):
    for char, digit in zip(chars[a], digits[a]):
        print(formatter.format(char, digit))

which gives

  chars   digits
-----------------
      A        0
      C        2
      E        4
      G        6
      I        8

Comments

0

Assume that your two lists have data as follows, then the following code will do the trick

# for python3
Char=['A', 'B', 'C', 'D', 'E']    # these lists can have as many elements as you want
Numb=[1,2,3,4,5]

for x,y in zip(Char, Numb):       #zip stops when the shorter list ends
  print (x +" "+ str(y) )

Output

sh-4.2# python3 main.py                                                                                                                                                         
A 1                                                                                                                                                                             
B 2                                                                                                                                                                             
C 3                                                                                                                                                                             
D 4                                                                                                                                                                             
E 5  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.