Generating a random codes Python_

Built-in functions chr () ord ()

Both built-in function is used to decimal (hexadecimal may be) and the conversion between ASCii

chr (): corresponds to a decimal by a letter or a symbol ASCii

t_1 = chr(99)
t_2 = chr(66)
print(t_1)
print(t_2)
#c
#B

ord (): to convert into a letter or symbol corresponding to ASCii int

t_3 = ord('c')
t_4 = ord('B')
print(t_3)
print(t_4)
#66
#99

Randomly generated codes

import random
def get_random_code(length = 4):
    data =[]
    for i in range(length):
        temp_code = random.randint(65,90)
        data.append(chr(temp_code))
    return ''.join(data)
get_random_code()
#'NWVI'

.join () connect function

str.join ( sequence ) sequences with sequence connected str

list_1 = ['aa','bb','cc']
str = '-'
get_join = str.join(list_1)
print(get_join)
#aa-bb-cc

List to be connected can not have otherwise int error occurs

 

Guess you like

Origin www.cnblogs.com/wangxiaobei2019/p/11619163.html