How to Generate a Random Strings Base on Input in Python

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.  
  5.  
  6. print("\n\n============= Generate a Random Strings Base on Input =============")
  7.  
  8. print("Your target string is: sourcecodester\n")
  9.  
  10. input("Press enter to start....")
  11.  
  12. possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' ., !?;:'
  13.  
  14.  
  15. t = "sourcecodester"
  16.  
  17. attemptThis = ''.join(random.choice(possibleCharacters)
  18.                                                                 for i in range(len(t)))
  19. attemptNext = ''
  20.  
  21. completed = False
  22. iteration = 0
  23.  
  24.  
  25. while completed == False:
  26.         print(attemptThis)
  27.        
  28.         attemptNext = ''
  29.         completed = True
  30.        
  31.        
  32.         for i in range(len(t)):
  33.                 if attemptThis[i] != t[i]:
  34.                         completed = False
  35.                         attemptNext += random.choice(possibleCharacters)
  36.                 else:
  37.                         attemptNext += t[i]
  38.                        
  39.  
  40.         iteration += 1
  41.         attemptThis = attemptNext
  42.         time.sleep(0.1)
  43.  
  44. print("Target found after " +
  45.         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:

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

Tags

Add new comment