如何显示for循环中的一串整数和字母 [英] How to display a string of integers and letters from a for loop

查看:95
本文介绍了如何显示for循环中的一串整数和字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在33和126之间生成8个随机数来创建8个字符的密钥,然后将8个数字中的每一个转换为ASCII字符.我已经能够做到所有这些:

I am trying to create an eight-character key by generating 8 random numbers between 33 and 126, then convert each of the 8 numbers into an ASCII character. I have been able to do all that:

print('Eight-Character Key: ')    
for i in range(0,8):
            key = random.randint(33,126)
            key = chr(key)
            print(key,end='')

输出为:

Eight-Character Key: 
BAU78)E)

但理想情况下,我希望它全部位于同一行:

But ideally I would like it to be all on the same line:

Eight-Character Key: BAU78) E)

推荐答案

在第一个print语句后放置end

print('Eight-Character Key: ',end='')    
for i in range(0,8):
            key = random.randint(33,126)
            key = chr(key)
            print(key,end='')

其他方法-通过将密钥附加到字符串,然后print

Other ways to do it - By appending the key to a string and then print

k = ''
for i in range(0,8):
            key = random.randint(33,126)
            k += chr(key) 
print('Eight-Character Key: ',k,end='') 

这篇关于如何显示for循环中的一串整数和字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆