from random import shuffle def shuffle_str(s): # 将字符串转换成列表 str_list = list(s) # 调用random模块的shuffle函数打乱列表 shuffle(str_list) # 将列表转字符串 return ''.join(str_list) # 调用 if __name__ == '__main__': for i in range(5): print(shuffle_str('hello,world!'))
输出结果:
!lrw,hloldeo
!doorwhelll,
w,rhlloe!dol
dlehl!lo,orw
whl,!oldrloe