gpt4 book ai didi

python - 如何在数字 2500 前添加括号?

转载 作者:行者123 更新时间:2023-12-02 19:18:10 28 4
gpt4 key购买 nike

我正在尝试输出以下两行文本:

The city with the most contracts is:

Chicago (2500 contracts)

这是我正在使用的代码:

a = 'Chicago'
b = 2500
print('The city with the most contracts is:')
print(a, b, 'contracts)')

当前输出是:

The city with the most contracts is:
Chicago 2500 contracts)

如何在数字 2500 前添加括号?

我尝试将括号添加为字符串,如下所示:

print('The city with the most contracts is:')
print(a, '(', b, 'contracts)')

但它在括号和数字 2500 之间添加了一个不需要的空格,如下所示:

The city with the most contracts is:
Chicago ( 2500 contracts)

最佳答案

最简单的方法是使用格式化字符串。

print(f'{a} ({b} contracts)')

关于python - 如何在数字 2500 前添加括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63400134/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com