remove the spaces

Question:

#!/usr/bin/python
import random
lower_a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_a = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

all = []
all = "".join("".join(lower_a) + "".join(upper_a) + "".join(num))
all = all.split()
x = 0
while x < 10:
    for i in range(7):
        a = random.choice(all)
        print a,
    print
    x += 1

What I want to do is remove the spaces from the output. What it gives now is:

Z 3 a A I K R
G B i N 9 c E
v g E r A N 8
e B 6 d v H O
c a V 8 c x y
b g 2 W a T T
f 8 H T r 6 E
p D K l 5 p u
x q 8 P Z 9 T
n I W X n B Q
Asked By: tekknolagi

||

Answers:

instead of:

 for i in range(7):
  a = random.choice(all)
  print a,
 print

you could do this:

 print "".join(random.choice(all) for x in range(7))
Answered By: Tarnay Kálmán

nevermind – i fixed it

while x < 10:
    y = []
    for i in range(7):
        a = random.choice(all)
        y.append(a)
    print "".join(y)
    x += 1
Answered By: tekknolagi

Putting it all together with the string module.

import random
import string

chars = string.ascii_uppercase + string.ascii_lowercase + string.digits

for row in range(10):
    print ''.join(random.choice(chars) for col in range(7))
Answered By: Brian Goldman

The space is placed by print a,. A comma following print statement print a space.

Answered By: xiaowl
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.