python 3.x – Generate random string to match the goal string with minimum iteration and control statements

It took me a while to understand the code but I hope that I understood it properly.
Lets say you have the string “abcd”

   goal string      "abcd"
   1st iteration    "ftcP" -> you save the 3rd position as found
   2nd iteration    "aXlL" -> At this pint both 1st and 3rd positions have been found 
   3rd iteration    "...." -> Still 3rd and 1st 
   4th iteration    "UbDd" -> all positions have matched at least once. 
   Stop and return 4 as the result

If the previous example is what you want to achieve below is my code suggestion

from random import choice

pool = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !@#&()–({}):;‘,'?/*1234567890'''
GetNewChar = lambda x: choice(pool)

def GetRandomString(stringLen):
    characters = (GetNewChar() for i in range(stringLen))
    return ''.join(characters)

def IterationsToMatchString(goal_string):
    IsGoalAchieved = (False for i in goal_string)
    while not all(IsGoalAchieved): 
        iterations += 1
        test_string = GetRandomString(len(goal_string))
        Matches = (g==t for g,t in zip(goal_string,test_string))
        for i,t in enumerate(IsGoalAchieved):
            if t is False: 
                IsGoalAchieved(i) = Matches(i)
    return iterations

example = input()

regardless of which variable naming convention you use I suggest not using the same name for the function and the output unless you are writing a decorator function. In your “randomString” function you return a variable named “randomString” which makes the code difficult to understand and is likely to bug. I would suggest changing the name of the function to improve reading.

Also you abuse the global keyword. You can just pass arguments to the function. In this case your function randomString generates a random string of a fixed length. Therefore you can always call that function with the length as a parameter.

Also as you have seen I have substituted the usage of randrange by choice (but does the same). I prefer it because we are picking one item out of a certain sequence.I have used a lambda function to provide a more “readable” name that gives information about what is being done.