首页 > 解决方案 > 如何将int变量转换为字符串

问题描述

这个问题是关于如何将 int 转换为字符串。我是编码新手,我不知道如何将 int 值转换为 str 值,因为在 python 中,我们不能将 int 值与字符串值结合起来。所以我们必须将 int 值转换为 string 。希望我的问题能帮助我自己和未来的程序员。

print("Type your age ")
    character_age = input()
    print("So your age is "+character_age)
    print("If your father is 20 years older what will be your fathers age ?")
    age_character = int(character_age)
    print("Father's age is " +age_character)

标签: python

解决方案


输入:

print("Type your age ")
character_age = input()
print("So your age is "+character_age)
print("If your father is 20 years older what will be your fathers age ?")
age_character = int(character_age)+20 #add 20 with character age
print("Father's age is " + str (age_character )) # convert back into string for print

输出:

Type your age
5
So your age is 5
If your father is 20 years older what will be your fathers age ?
Father's age is 25

推荐阅读