相关疑难解决方法(0)

Python的打印方式:使用'格式'还是百分比形式?

在Python中,似乎有两种不同的生成格式化输出的方法:

user = "Alex"
number = 38746
print("%s asked %d questions on stackoverflow.com" % (user, number))
print("{0} asked {1} questions on stackoverflow.com".format(user, number))
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以优先于另一种方式?它们是等价的,有什么区别?应该使用什么形式,特别是对于Python3?

python printing format

94
推荐指数
4
解决办法
8万
查看次数

动态格式化字符串

如果我想让我的格式化字符串动态可调,我将更改以下代码

print '%20s : %20s' % ("Python", "Very Good")
Run Code Online (Sandbox Code Playgroud)

width = 20
print ('%' + str(width) + 's : %' + str(width) + 's') % ("Python", "Very Good")
Run Code Online (Sandbox Code Playgroud)

但是,似乎字符串连接在这里很麻烦.还有其他简化方法吗?

python string

45
推荐指数
4
解决办法
3万
查看次数

标签 统计

python ×2

format ×1

printing ×1

string ×1