gpt4 book ai didi

python - 在 Python 中打印两个数字的数字连接

转载 作者:太空狗 更新时间:2023-10-30 01:39:20 24 4
gpt4 key购买 nike

有没有办法在 Python 中连接数字,假设我有代码

print(2, 1)

我希望它打印 21,而不是 2 1,如果我使用“+”,它会打印 3。有办法做到这一点吗?

最佳答案

使用字符串格式化操作符:

print "%d%d" % (2, 1)

编辑:在 Python 2.6+ 中,您还可以使用字符串的 format() 方法:

print("{0}{1}".format(2, 1))

关于python - 在 Python 中打印两个数字的数字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9472410/

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