Return Python 中的格式化字符串
Return formatted string in Python
我有一个字符串:
testString = """ My name is %s
and I am %s years old
and I live in %s"""
我有代码可以找到我想输入到 testString 中的这三个字符串。我怎么做?在 Java 我会做:
return String.format(testString, string1, string2, string3)
在Python中有对应的吗?
这是使用%
的版本:
print """ My name is %s
and I am %s years old
and I live in %s""" % ('tim', 6, 'new york')
这是我的首选版本:
testString = """ My name is {}
and I am {} years old
and I live in {}""".format(string1, string2, string3)
您可能想阅读 https://docs.python.org/3.4/library/string.html#string-formatting。
我有一个字符串:
testString = """ My name is %s
and I am %s years old
and I live in %s"""
我有代码可以找到我想输入到 testString 中的这三个字符串。我怎么做?在 Java 我会做:
return String.format(testString, string1, string2, string3)
在Python中有对应的吗?
这是使用%
的版本:
print """ My name is %s
and I am %s years old
and I live in %s""" % ('tim', 6, 'new york')
这是我的首选版本:
testString = """ My name is {}
and I am {} years old
and I live in {}""".format(string1, string2, string3)
您可能想阅读 https://docs.python.org/3.4/library/string.html#string-formatting。