如何在python中用字符串附加用户输入(整数)?

时间:2019-12-12 03:26:43

标签: python append

rep_number = int(input('X time(s)'))

ther_note.append(+ rep_number + "time(s).")
  

TypeError:+不支持的操作数类型:“ int”和“ str”

2 个答案:

答案 0 :(得分:2)

您不能将字符串和整数附加为一个元素,因此我认为最简单的方法是使用str(rep_number)将整数转换为字符串。您还可以使用format()("{0} times").format(rep_num)生成为字符串。

答案 1 :(得分:2)

您无法在示例中进行操作,但是有很多方法。如果您只是要输出输入而不会将其用作实际整数,则可以使用f-string

附加格式化的字符串
ther_note = []
rep_number = int(input('X time(s)'))
ther_note.append(f"{rep_number} times(s)")

如果以后需要使用数字,请存储数字,并在以后需要时添加文本。

ther_note = []
rep_number = int(input('X time(s)'))
ther_note.append(rep_number)

for x in ther_note:
    print(f"{x + 100} times(s)")

添加intsstrs的其他方法:

num = 10
sentence = "My age is "
print(sentence + str(num))

num = 10
sentence = "My age is {}".format(num)
print(sentence)