Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 398
sixpain Avatar asked Jun 09 '17 16:06

sixpain


1 Answers

Use string formatting:

s = "Your first value is {} your second value is {}".format(amount1, amount2)

This will automatically handle the data type conversion, so there is no need for str().

Consult the Python docs for detailed information:

https://docs.python.org/3.6/library/string.html#formatstrings

like image 154
JakeD Avatar answered Sep 20 '22 06:09

JakeD