如何在Python 3.7中随机放置字符串

问题描述 投票:-1回答:1

我正在生成随机数,并尝试将其添加到现有密码中。但是我不知道如何在字符串的前面或后面随机化随机数的位置。

任何方式吗?

python python-3.x string random
1个回答
0
投票

这应该可以帮助您入门。

import random

# Reproducible example
random.seed(1)

# Setup
rand_num = 1234
rand_pw = "RandomPassword"

# Get random boolean and insert in front or behind
if bool(random.getrandbits(1)):
    print(str(rand_num) + rand_pw)
else:
    print(rand_pw + str(rand_num))

生产

RandomPassword1234
© www.soinside.com 2019 - 2024. All rights reserved.