python中不同选项的字符串格式

python中不同选项的字符串格式,python,string-formatting,Python,String Formatting,我想打印出一个字符串,该字符串描述从列表中删除了多少项(“计数”),以及列表中还剩下多少项。为了使其语法正确,我有以下代码: if count == 1: print("\n{} definition was removed. We therefore currently have {} definitions to test...".format(count, len(test_list))) if count > 1: print("\n{} definitions w

我想打印出一个字符串,该字符串描述从列表中删除了多少项(“计数”),以及列表中还剩下多少项。为了使其语法正确,我有以下代码:

if count == 1:
    print("\n{} definition was removed. We therefore currently have {} definitions to test...".format(count, len(test_list)))
if count > 1:
    print("\n{} definitions were removed. We therefore currently have {} definitions to test...".format(count, len(test_list)))    

有没有一种更类似于python的方法来实现这一点?

在您的例子中,您只有一个变量参数was/were,它会随着count的值而变化。为了使代码更具可读性,您可以只更改变量,而不是格式化整个消息

msg = '"\n{} {} removed. We therefore currently have {} definitions to test..."'
prep = 'definition was'
if count > 1:
    prep = 'definitions were'

print(msg.format(count, prep, len(test_list)))

欢迎来到本地化世界,你能用几句话解释一下你的解决方案吗?这样的答案总是更有力。这没有考虑到世界“定义”的多元化@BarunSharma你是什么意思?