Home > database >  Question with type() conversion in python 3
Question with type() conversion in python 3

Time:12-28

name = input("what is your name : ")
age = int(input("what is your age : "))
age_after_100_years = 2021   (100-age)
print(name   " your age after 100 years is "   age_after_100_years)

in the above code on line 2, ive converted the string to int and then used it in "age_after_100_years" variables, but it gives me an error

Output:
what is your name : p
what is your age : 25
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-565602469d9c> in <module>
  2 age = int(input("what is your age : "))
  3 age_after_100_years = 2021   (100-age)
----> 4 print(name   " your age after 100 years is "   age_after_100_years)

TypeError: can only concatenate str (not "int") to str

But when i use str() on line 3 i.e

name = input("What is your name? : ")
age = int(input("What is your age? : "))
age_after_100_years = str((2022-age) 100)
print(name " you will be 100 years old by the year " age_after_100_years)

Output:
What is your name? : pratik
What is your age? : 25
pratik you will be 100 years old by the year 2097

the above code works,

as i want to know, i've already converted the string to int() in variable "age", then why do i have to convert variable "age_after_100_years" to str(), if my "age" variable is int() and "age_after_100_years" has int() inputs to begin with, and i am concatenating int with int inputs?

CodePudding user response:

The variable "age_after_100_years" is int, so you have to convert it to string if you want to concatenate it to another string using " " operator.

CodePudding user response:

The operator in Python does addition for numerical values (for example, 1 2 = 3) but it does concatenation for strings ("1" "2" = "12"). From what Python is telling you, you're attempting to a string and a number, which is not allowed. Instead, you need to convert the number to a string value (str(number_variable)) before doing a concatenation.

Or, as others have noted, use f-strings, which allow you to substitute your numbers (or any other types/objects which support str()) into a string expression (actually, anything, but you might get text such as "BinaryTree object at 0x0454354" if you call str() on a BinaryTree class that has not implemented an interface for the str() method).

Looking at your example, you might want to do:

name = input("What is your name? : ")
age = int(input("What is your age? : "))
age_after_100_years = str((2022-age) 100)

# passing multiple parameters to print()
print(name, " you will be 100 years old by the year ", age_after_100_years)

# using string concatenation
print(name   " you will be 100 years old by the year "   str(age_after_100_years))

# using f-strings
print(f"{name} you will be 100 years old by the year {age_after_100_years}")

# using %-formatting
print("%s you will be 100 years old by the year %s" % (name, age_after_100_years) )

# using string.format()
print("{} you will be 100 years old by the year {}".format(name, age_after_100_years) )

All should get the job done, but f-strings are cleaner, shorter, and easier to read :)

CodePudding user response:

friend your code id giving an error because the age variable in it is of int type and in the next line you are concatenating the int which will not be allowed So I have corrected your code and after correction this is the code

name = input("what is your name : ")
age = int(input("what is your age : "))
age_after_100_years = 2021   (100-age)
print(name , " your age after 100 years is " , age_after_100_years)

and the result after that is

what is your name : p
what is your age: 12
p  your age after 100 years is  2109
  • Related