• We just launched and are currently in beta. Join us as we build and grow the community.

How to Generate a Random Strings Base on Input in Python

GBHacker23

Ad Metrics Pro
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
98
Likes
74
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we will program 'How to Generate Random Strings Based on Input in Python'. We will learn how to generate a string based on user input. The objective is to enable you to efficiently generate a random string with specific characteristics provided by the user. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you can do it yourself with ease. The program I will show you covers the basics of programming for generating random strings based on user input. I will do my best to provide you with a simple method for generating random strings. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import

    string
  2. import

    random
  3. import

    time


  4. print

    (

    "\n

    \n

    ============= Generate a Random Strings Base on Input ============="

    )

  5. print

    (

    "Your target string is: sourcecodester\n

    "

    )

  6. input

    (

    "Press enter to start...."

    )

  7. possibleCharacters =

    string

    .ascii_lowercase

    + string

    .digits

    + string

    .ascii_uppercase

    + ' ., !?;:'


  8. t =

    "sourcecodester"

  9. attemptThis =

    ''

    .join

    (

    random

    .choice

    (

    possibleCharacters)
  10. for

    i in

    range

    (

    len

    (

    t)

    )

    )
  11. attemptNext =

    ''

  12. completed =

    False
  13. iteration =

    0


  14. while

    completed ==

    False

    :
  15. print

    (

    attemptThis)

  16. attemptNext =

    ''
  17. completed =

    True


  18. for

    i in

    range

    (

    len

    (

    t)

    )

    :
  19. if

    attemptThis[

    i]

    !=

    t[

    i]

    :
  20. completed =

    False
  21. attemptNext +=

    random

    .choice

    (

    possibleCharacters)
  22. else

    :
  23. attemptNext +=

    t[

    i]


  24. iteration +=

    1
  25. attemptThis =

    attemptNext
  26. time

    .sleep

    (

    0.1

    )

  27. print

    (

    "Target found after "

    +
  28. str

    (

    iteration)

    + " iterations"

    )

The script attempts to generate a string that matches the target "sourcecodester" by iteratively generating and comparing random strings. It continues until it successfully matches the target string, printing each attempt and the total number of iterations taken to achieve the match.

Output:

how-to-generate-a-random-strings-base-on-input-in-python-1.png

The How to Generate a Random Strings Base on Input in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Generate a Random Strings Base on Input in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top