How to print the characters horizontally?
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 | def rand_gen1(): import random for i in range(8): print random.choice('0123456789abcdefghijklmnopqrstuvwxyz') |
上面的代码在垂直方向上打印一个8个随机字符或整数的字符串。我需要它像
我需要做什么改变?
或者像这样(对于从字符串导入的Richard Neumann):
1 2 3 4 5 6 7 | import random from string import ascii_lowercase, digits def rand_gen3(): print(''.join(random.choice(ascii_lowercase + digits) for _ in range(8))) rand_gen3() |
1 2 3 4 5 6 7 8 9 10 | from random import choice from string import ascii_lowercase, digits def rand_gen(length=8, pool=digits+ascii_lowercase): return ''.join(choice(pool) for _ in range(length)) print rand_gen() |
您需要将随机选择存储在一个字符串中,然后打印该字符串:
1 2 3 4 5 6 7 | import random def rand_gen1(): rand_str = '' for i in range(8): rand_str += random.choice('0123456789abcdefghijklmnopqrstuvwxyz') print rand_str |
每个
现在,您可以使用
1 2 3 4 5 6 | import random def rand_gen1(): output ="" for i in range(8): output += random.choice('0123456789abcdefghijklmnopqrstuvwxyz') print output |