How to print the characters horizontally?
This question already has an answer here:
Right now you're printing out everything on its own new line by using print
. You can make these all into one string by storing the values in a string variable.
import random
def rand_gen1():
output = ""
for i in range(8):
output += random.choice('0123456789abcdefghijklmnopqrstuvwxyz')
print output
或者像那样(Richard Neumann用于从字符串导入):
import random
from string import ascii_lowercase, digits
def rand_gen3():
print(''.join(random.choice(ascii_lowercase + digits) for _ in range(8)))
rand_gen3()
You need to store the random choices in a string and then print the string:
import random
def rand_gen1():
rand_str = ''
for i in range(8):
rand_str += random.choice('0123456789abcdefghijklmnopqrstuvwxyz')
print rand_str
Each print
statment creates a new line.
上一篇: 在两个字符串之间不打印换行符
下一篇: 如何水平打印字符?