Random strings in Python 2.6 (Is this OK?)

前端 未结 5 1735
独厮守ぢ
独厮守ぢ 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:40

    Sometimes a uuid is short enough and if you don't like the dashes you can always.replace('-', '') them

    from uuid import uuid4
    
    random_string = str(uuid4())
    

    If you want it a specific length without dashes

    random_string_length = 16
    str(uuid4()).replace('-', '')[:random_string_length]
    

提交回复
热议问题