python中怎样从一些字符串中随机选择一个输出

如题所述

以下是示例代码:

import random
str1='this_a_string_with_several_characters'
n=random.randint(0,len(str1))
print(str1[n])#返回一个字符串随机位置的字符
strs=['str1','str2','str3','str4','str5','str6']
n=random.randint(0,len(strs))
print(strs[n])#返回一个字符串数组随机位置的字符串

不明白可追问

温馨提示:答案为网友推荐,仅供参考
相似回答