for i in range(0, 9): globals()[f"my_variable{i}"] = f"Hello from variable number {i}!" print(my_variable3) # Hello from variable number 3!
Here is what the above code is Doing:
1. We create a for loop that iterates 9 times.
2. We create a new variable using the f-string syntax.
3. We assign the new variable a value.
4. We print the value of the third variable.
You can use this same technique to create a dictionary of variables.