如何在 Python 中连接字符串和数字? [英] How can I concatenate a string and a number in Python?

查看:74
本文介绍了如何在 Python 中连接字符串和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Python 中连接一个字符串和一个数字.当我尝试这个时,它给了我一个错误:

I was trying to concatenate a string and a number in Python. It gave me an error when I tried this:

"abc" + 9

错误是:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    "abc" + 9
TypeError: cannot concatenate 'str' and 'int' objects

为什么我不能这样做?

如何在 Python 中连接字符串和数字?

How can I concatenate a string and a number in Python?

推荐答案

Python 是强类型.没有隐式类型转换.

Python is strongly typed. There are no implicit type conversions.

您必须执行以下操作之一:

You have to do one of these:

"asd%d" % 9
"asd" + str(9)

这篇关于如何在 Python 中连接字符串和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆