Random strings in Python 2.6 (Is this OK?)

前端 未结 5 1738
独厮守ぢ
独厮守ぢ 2020-12-12 12:54

I\'ve been trying to find a more pythonic way of generating random string in python that can scale as well. Typically, I see something similar to

\'\'.join(r         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 13:46

    import os
    random_string = os.urandom(string_length)
    

    and if you need url safe string :

    import os
    random_string = os.urandom(string_length).hex() 
    

    (note random_string length is greatest than string_length in that case)

提交回复
热议问题