使用Python的Titular()函数生成随机标题
发布时间:2024-01-13 05:48:55
Titular()是一个Python函数,用于生成随机标题。它可以用于创建假新闻标题、随机测试数据标题等等。以下是Titular()函数的使用示例:
import random
def Titular():
# 随机选择一个动词
verbs = ['Study', 'Explore', 'Discover', 'Investigate', 'Uncover']
verb = random.choice(verbs)
# 随机选择一个名词
nouns = ['Secrets', 'Mysteries', 'Truths', 'Conspiracies', 'Legends']
noun = random.choice(nouns)
# 随机生成一个数字
number = random.randint(1, 100)
# 构建标题字符串
title = f"{verb} the {noun} {number}!"
return title
# 生成1000个随机标题
for _ in range(1000):
title = Titular()
print(title)
上述代码会输出1000个随机标题,每个标题都由一个随机的动词、一个随机的名词和一个随机的数字组成。下面是几个示例输出:
Study the Secrets 42! Explore the Conspiracies 99! Uncover the Legends 7! Discover the Mysteries 56! Investigate the Truths 23!
你可以根据需要,添加更多的动词和名词到verbs和nouns列表中,以扩展生成随机标题的范围。
