Python中的随机字符串 [英] Random strings in Python
本文介绍了Python中的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Python 中创建随机字符串?
我需要它是数字然后重复字符直到你完成这是我创建的
def random_id(length):数字 = '0123456789'阿尔法 = 'abcdefghijklmnopqrstuvwxyz'id = ''对于范围内的 i (0,length,2):id += random.choice(number)id += random.choice(alpha)返回 ID
解决方案
从(例如)小写字符生成字符串:
导入随机,字符串定义随机字(长度):字母 = string.ascii_lowercase返回 ''.join(random.choice(letters) for i in range(length))
结果:
<预><代码>>>>随机词(10)'vxnxikmhdc'>>>随机词(10)'ytqhdohksy'How do you create a random string in Python?
I needed it to be number then character repeat till you're done this is what I created
def random_id(length):
number = '0123456789'
alpha = 'abcdefghijklmnopqrstuvwxyz'
id = ''
for i in range(0,length,2):
id += random.choice(number)
id += random.choice(alpha)
return id
解决方案
Generating strings from (for example) lowercase characters:
import random, string
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
Results:
>>> randomword(10)
'vxnxikmhdc'
>>> randomword(10)
'ytqhdohksy'
这篇关于Python中的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文