python - STR 函数不起作用

标签 python string

我对编程非常陌生。我有下面的问题,我试图列出旁边有数字的人的名字。所以在这里,我试图将“史蒂夫”加入“100”。我希望结果是 steve100。培训视频说您不能连接字符串和整数,因此您必须键入 str(grades[1]) 才能将 100 连接到 steve。但是,它对我不起作用,请参见下文。

>>> grades = ["steve", 100, "john", 50]
>>> grades[0] + grades[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> grades[0] + str(grades[1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> 

最佳答案

您需要将 int 解析为字符串,并且您正在使用 str 函数正确执行此操作:

grades[0]+str(grades[1])

您收到错误 str object is not callable 可能是因为您之前定义了一个 str 变量。

你可以运行 del str 然后再试一次,它会成功 =)

关于python - STR 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013289/

相关文章:

python - 如何在 Python 中将字符串转换为 "command"?

Java,用于验证权限的字符串匹配

regex - 如何在 Scala 中删除两个特定字符之间的子字符串

python - 如果几个字符串之一在其他字符串中 : return a value + matching string, Python

Python将html插入MySQL

python - PIL 中的 PNG 图像质量

python - 我如何在 PyQt5 中销毁并重新创建我的窗口?

带有传递的python函数

python - 如何使用多维 numpy 数组作为输入使用 Seaborn 绘制箱线图或 fiddle 图?

string - 在 Python 3 中检查字符串是否包含重复字符的最快方法是什么?