关于python:如何生成随机字符串?

How to generate random string?

本问题已经有最佳答案,请猛点这里访问。

我只是想问一下,比如从:Random.randint(a, b)中取随机数。

我只想问一下,如何像randint一样取随机字符串,但这次是用随机字符串。还有吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#This program will play a little game

import random

a = ''
b = ''
c = ''
d = ''
e = ''
f = ''

print('Hi. Please enter your name in letters')
name = str(input())
print('Hi ' + name + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.')
print('But do not worry. I am going to let you to enter 6 names and i will choose one of them.')
print('After that you have to answer the correct name that i am thinking right now')
print('Please enter the first name in letters')
name1 = str(input())
print('Please enter the second name in letters')
name2 = str(input())
print('Please enter the third name in letters')
name3 = str(input())
print('Please enter the fourth name in letters')
name4 = str(input())
print('Please enter the fifth name in letters')
name5 = str(input())
print('Please enter the sixth name in letters')
name6 = str(input())
name1 = a
name2 = b
name3 = c
name4 = d
name5 = e
name6 = f

print('Alright ' + name + ' . Thank you for entering the names.')
secretname = random.randint(a, f)
for i in range(2):
        print('Now enter the name that i am thinking of.')
        ans = str(input())
        if ans != secretname:
                print('Wrong. Guess another name')

if ans == secretname:
        print('Good job ' + name)
else:
        print('Wrong. The name i was thinking of was ' + secretname)

这是一个小游戏,要求你输入6个名字,然后游戏会猜到你输入的6个数字之间的一个数字,但它总是给我一个错误。希望使用随机字符串。

我的工作版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import random

print('Hi. Please enter your name in letters')
yourname = str(input())
print('Hi ' + yourname + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.
But do not worry. I am going to let you to enter 6 names and i will choose one of them.
After that you have to answer the correct name that i am thinking right now
Please enter the first name in letters'
)

name = ["","","","","",""]
number = ["first","second","third","fourth","fifth","sixth"]


def insert(n):
    temp = name.copy()
    name[n] = str(input("name" + str(n + 1) +":"))
    if name[n] in temp:
        print("[error]: ---> This name exists yet, try again")
        print(">>>", end='')
        insert(n)

for n in range(6):
    insert(n)
    print('Please enter the ' + number[n] + ' name in letters')

print('Alright ' + yourname + ' . Thank you for entering the names.')
secretname = name[random.randint(0, 6)]
print("Soluzion:", secretname)
print("-" * 10)
for i in range(2):
    print('Now enter the name that i am thinking of.')
    ans = str(input())
    if ans != secretname:
        print('Wrong. Guess another name')

if ans == secretname:
    print('Good job ' + yourname)
else:
    print('Wrong. The name i was thinking of was ' + secretname)

产量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Hi. Please enter your name in letters Gio Hi Gio. I am going to play a
little game. In this game you have to guess a specific name i am
thinking right now. But do not worry. I am going to let you to enter 6
names and i will choose one of them. After that you have to answer the
correct name that i am thinking right now Please enter the first name
in letters
name 0: Giovanni
Please enter the first name in letters
name 1: Marta
Please enter the second name in letters
name 2: Giovanni
[error]: ---> This name exists yet, try again
>>>name 2: Giovanni
[error]: ---> This name exists yet, try again
>>>name 2: Carlo
Please enter the third name in letters
name 3: Mimmo Please enter the fourth name in letters
name 4: June
Please enter the fifth name in letters
name 5: Caterina Please enter the sixth name in letters
Alright Gio . Thank you for entering the names.
Solution: Mimmo
----------
Now enter the name that i am thinking of.
M
Wrong. Guess another name Now enter the name that i am thinking of.
Mimmo
Good job Gio


1
2
3
4
5
6
7
import string
import random
def random_string(length):
    return ''.join(random.choice(string.ascii_letters) for m in xrange(length))

print random_string(10)
print random_string(5)

输出:

1
2
'oJyPiEbhka'
'XXwuA'

你好,Husnain先生,

CR()函数

CHR(I)返回表示其Unicode码位为整数i的字符的字符串。例如,chr(97)返回字符串"a",而chr(8364)返回字符串"€"。这与ORD()相反。

参数的有效范围是从0到1114111(以16为基数为0x10ffff)。如果超出该范围,将引发ValueError。

解决方案

尝试下面的代码,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#This program will play a little game

import random

a = ''
b = ''
c = ''
d = ''
e = ''
f = ''

print('Hi. Please enter your name in letters')
name = raw_input()


print('Hi ' + name + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.')
print('But do not worry. I am going to let you to enter 6 names and i will choose one of them.')
print('After that you have to answer the correct name that i am thinking right now')
print('Please enter the first name in letters')
name1 = raw_input()

print('Please enter the second name in letters')
name2 = raw_input()

print('Please enter the third name in letters')
name3 = raw_input()

print('Please enter the fourth name in letters')
name4 = raw_input()

print('Please enter the fifth name in letters')
name5 = raw_input()

print('Please enter the sixth name in letters')
name6 = raw_input()

print('Alright ' + name + ' . Thank you for entering the names.')

lists1 = [name1,name2,name3,name4,name5,name6]

secretname = lists1[random.randint(0,len(lists1))]

for i in range(2):
        print('Now enter the name that i am thinking of.')
        ans = raw_input()
        if ans != secretname:
                print('Wrong. Guess another name')

if ans == secretname:
     print('Good job ' + name)
else:
    print('Wrong. The name i was thinking of was ' + secretname)

我希望我的回答有帮助。如果有任何疑问,请评论。


更新:

对于您描述的特定情况,您可以简单地创建一个包含您允许的所有字符的字符列表。然后,您可以使用该命令创建长度为X的单词(替换为实际长度:

1
2
3
base_chars = [ 'a', 'b', 'c', '1', '2', '3' ]
word_length = 10 #change this to the desired word length
[random.choice(base_chars) for _ in range(word_length)]

原件:随机字符串-定义字符

您应该首先决定要使用哪种字符。例如,如果您想使用0到127之间的ASCII字符,或者只使用A-Z和A-Z字符,还需要考虑数字、空格等,因此您应该首先确定要从中随机选择的字符集。

随机选择角色

如果要在ASCII表中使用0到127范围,则可以使用此选项:

char = chr(random.randint(0, 127))

生成随机词

现在,要创建一个单词,首先应该确定它的大小。这也可以是随机的。例如,我们将随机选择一个单词的大小(范围有限):

1
2
3
rand_str =""
for _ in range(10):
     rand_str += chr(random.randint(0, 127))

我建议使用一个包含输入名称的列表,而不是将名称保存到不同的变量中。它可能看起来像这样:

1
2
3
4
5
6
name_list = []
print('Please enter the first name in letters')
name_list.append(input())
print('Please enter the second name in letters')
name_list.append(input())
...

然后可以使用random.choice随机选择一个列表项,例如

1
2
import random
secretname = random.choice(name_list)

试试这个,

1
2
3
4
import string
alpha = string.ascii_uppercase
num = string.digits
''.join(random.choice(alpha + num) for _ in range(5)) #number you want

您可以使用random.randint:

1
2
3
my_string ="abcdefghijklmnopqrstuvwxyz"
index = random.randint(0,25)
letter = my_string[index]

你只要把它循环一下,就可以用字母造出字符串。