Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

How to use a variable to be displayed in a input prompt in python

  1. User

    How to use a variable to be displayed in a input prompt in python

  2. Assistant

    To display the value of a variable in an input prompt in Python, you can use string concatenation or string formatting. Here are a few examples:

    1. String concatenation:
    name = "Alice"
    age = 25
    user_input = input("Enter your name, " + name + ", and age, " + str(age) + ": ")
    
    1. String formatting using f-strings (available in Python 3.6 and above):
    name = "Alice"
    age = 25
    user_input = input(f"Enter your name, {name}, and age, {age}: ")
    
    1. String formatting using the format() method:
    name = "Alice"
    age = 25
    user_input = input("Enter your name, {}, and age, {}: ".format(name, age))
    

    All of these examples will display the value of name and age inside the input prompt. The user can then input their response accordingly.