shuffle string in python

后端 未结 4 2140
春和景丽
春和景丽 2020-11-27 17:59

I am looking for a function or short program that receives a string (up to 10 letters) and shuffles it.

4条回答
  •  鱼传尺愫
    2020-11-27 18:30

    Python Provides the various solutions to shuffle the string:

    1. External library: python-string-utils

    • first install the python-string-utils library
      • pip install python_string_utils
    • use string_utils.shuffle() function to shuffle string
    • please use the below snippet for it

    Code Snippet

    import string_utils
    print string_utils.shuffle("random_string")
    

    Output:

    drorntmi_asng
    

    2. Builtins Method: random.shuffle

    Please find the code below to shuffle the string. The code will take the string and convert that string to list. Then shuffle the string contents and will print the string.

    import random
    str_var = list("shuffle_this_string")
    random.shuffle(str_var)
    print ''.join(str_var)
    

    Output:

    t_suesnhgslfhitrfi_
    

    3. External Library: Numpy

    import numpy
    str_var = list("shuffle_this_string")
    numpy.random.shuffle(str_var)
    print ''.join(str_var)
    

    Output:

    nfehirgsu_slftt_his
    

提交回复
热议问题