randint_choice.py
# -*- coding: utf-8 -*-
""" 在算法竞赛中,用 python 编写随机数据生成器是太合适了。
下面代码生成一个长度在5~10,恰好包含一个大写字母,其它字符为小写字母的字符串.
"""
from random import randint, choice
from string import *
#help(string)
L = randint(4, 9)
s = ''.join([ choice(ascii_lowercase) for i in range(L)])
p = randint(0, len(s))
print(s[:p] + choice(ascii_uppercase) + s[p:])
#print(ascii_lowercase)
#print(ascii_uppercase)
#print(ascii_letters)
运行 python test_choise.py
该篇文章介绍了如何使用Python的random和string库在算法竞赛中生成一个长度5到10,包含一个大写字母,其余小写字母的字符串,展示了choice函数的用法。
1876

被折叠的 条评论
为什么被折叠?



