-1

This question is on how to convert int to string.I am a novice in coding and I do not know how to convert an int value into a str value because , in python we cannot combine an int value along with a string value.So we have to convert the int value into string . Hope my question will help myself and future coders.

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)
  • May i know what is the expected result? please added your input and output.. So i can help you :) – Abhijith Mar 20 '19 at 05:27
  • 1
    Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – t.m.adam Mar 20 '19 at 05:30
  • Try this : print("Father's age is \\(age_character)") – Nirbhay Singh Mar 20 '19 at 05:38

8 Answers8

2

Input:

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

Output:

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
Abhijith
  • 108
  • 11
0

You can just use int():

x = '10'
int(x)

If you want to print an integer variable, you need to convert it back to a string using str(), as on your last line:

print("Father's age is " + str(age_character))
Nathaniel
  • 3,230
  • 11
  • 18
0

print() is throwing TypeError. You cannot concatenate a string with an int. You would need to convert your int to a string using the str function

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 " +str(age_character))

Change print("Father's age is " +age_character) to:

print("Father's age is " +str(age_character))

or

print("Father's age is",age_character)
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
0

You're already correctly converting the string character_age to an integer with the int constructor, although to be able to concatenate with another string you'll need to convert it back to a string, and since the printed message says the father's age should be 20 years older, you'll want to add 20 to it before converting to a string:

age_character = int(character_age) + 20
print("Father's age is " + str(age_character))
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

What do you mean you are not getting the result you want ?

age_character = int(character_age)

The age_character variable already converted age into int. The only problem is you are trying to print a combination of string and int. You have convert it to string.

print(type(age_character))
print("Father's age is " + str(age_character) )
str028
  • 186
  • 10
0

You cannot print a integer directly by using '+' operator as both datatypes are different. You need to convert that to a string.Try the below it will work.

print("Father's age is " + str(age_character))

The remaining code is correct

Manoj biroj
  • 288
  • 1
  • 6
0

You can do this like that(no need to convert into Strings): make sure your (character_age must be an integer)

code: {{ print "So your age is {character_age}".format(character_age=character_age) }}

{{ print "Father's age is {age_character}".format(age_character=age_character) }}

0

Python's built in function int() can be used for this purpose.

>>> a = "10"
>>> int(a)
10

However, this works provided the string is convertible.

>>> a = "45.2"
>>> int(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '45.2'
>>> b = "eleven"
>>> int(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'eleven'

Link to int() documentation.

nishant
  • 896
  • 1
  • 8
  • 27