Python 實現隨機打亂字符串


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM