如何在python中的字符串中插入变量值 [英] How to insert a variable value in a string in python

查看:261
本文介绍了如何在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

这篇关于如何在python中的字符串中插入变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆