show a variable in an input in python

To show a variable in an input in Python, you can use string formatting to include the variable's value in the input prompt. Here's an example:

main.py
name = "Alice"
age = 25
my_input = input("Enter your name and age (separated by a space): ")
print(f"Your name is {name} and your age is {age}.")
146 chars
5 lines

In this example, we create two variables, name and age, that contain the values "Alice" and 25, respectively. We then prompt the user to enter their name and age using the input function, which stores the user's input as a string in the variable my_input.

Finally, we use string formatting to display the values of the name and age variables in a message to the user.

Note that we use the f prefix before the string to indicate that it is an f-string, which allows us to embed expressions inside curly braces ({}) that are evaluated at runtime.

gistlibby LogSnag