pythonstringinput

How to insert a variable value in a string in python


Here is a simple example

amount1 = input("Insert your value: ")
amount2 = input("Insert your value: ")
print "Your first value is", amount1, "your second value is", amount2

This is ok, but I would like to know if there is another method to include the variable in the string without concatenation or a comma.

Is this possible?


Solution

  • With Python 3.6+ (PEP498), you can use formatted string literals, also known as f-strings:

    amount1 = input('Insert your value: ')
    amount2 = input('Insert your value: ')
    
    print(f'Your first value is {amount1}, your second value is {amount2}')