如何在python中的字符串中插入变量值
问题描述:
这是一个简单的例子
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.
这可能吗?
答
使用字符串格式:
s = "Your first value is {} your second value is {}".format(amount1, amount2)
这将自动处理数据类型转换,因此不需要str()
.
This will automatically handle the data type conversion, so there is no need for str()
.
有关详细信息,请咨询Python文档:
Consult the Python docs for detailed information:
https://docs.python.org/3.6/library/string. html#formatstrings