欢迎访问宙启技术站
智能推送

使用letter()函数在Python中生成随机字母序列并进行编码解码的实例

发布时间:2024-01-12 12:59:27

import string

import random

def generate_random_string(length):

    # 生成随机字母序列

    letters = string.ascii_letters

    random_string = ''.join(random.choice(letters) for _ in range(length))

    return random_string

def encode(string):

    # 编码

    encoded_string = ''

    for char in string:

        encoded_string += str(ord(char)) + ' '

    return encoded_string.strip()

def decode(string):

    # 解码

    decoded_string = ''

    string = string.split()

    for num in string:

        decoded_string += chr(int(num))

    return decoded_string

# 生成随机字母序列

random_string = generate_random_string(10)

print('Random String:', random_string)

# 编码

encoded_string = encode(random_string)

print('Encoded String:', encoded_string)

# 解码

decoded_string = decode(encoded_string)

print('Decoded String:', decoded_string)