1

In Java, If I want to print an incremented int variable, I can do it as:

int age = scn.nextInt(); System.out.println("You are " + age + " years old going on " + (age+1)); 

Output:

21 You are 21 years old going on 22 

Is it possible to do the same in Python?

I tried the following, and none of them work.

age = input("How old are you?") print("You are " + age + " years old going on " + str(age+1)) print("You are " + age + " years old going on {}".format(age+1)) print("You are " , age , " years old going on " , str(age+1)) print("You are %d years old going on %d" %(age, age+1)) print("You are " + str(age) + " years old going on " + str(age+1)) 

I have already tried the solutions provided in these links:

Print Combining Strings and Numbers

Concatenating string and integer in python

Converting integer to string in Python?

TypeError: Can't convert 'int' object to str implicitly

3
  • input gives you a string. You have to convert it to an integer before you add 1: age = int(input... Commented Aug 10, 2016 at 10:22
  • Even though an answer has already been accepted, please feel free to contribute answers here. I will still accept/upvote your good and informative solution. Commented Aug 10, 2016 at 10:40
  • "I tried the following, and none of them work." This is because age is a string, not an integer. To solve the problem correctly, it is necessary to read an integer, use integers to do the math, and then convert back to string for concatenation (or format back into the string). This question needs more focus, and should be deleted; there are really two problems in one, and it results from a failure to debug or produce a minimal reproducible example. Commented Jul 27, 2022 at 3:39

3 Answers 3

2

You need to convert the input to an int :

>>> age = int(input("How old are you?")) 

And then the following work :

print("You are " , age , " years old going on " , str(age+1)) print("You are %d years old going on %d" %(age, age+1)) print("You are " + str(age) + " years old going on " + str(age+1)) 
Sign up to request clarification or add additional context in comments.

3 Comments

I see, I was mislead by the error message from Python. All the while I thought it complains that int cannot be converted to string and in actual fact, it can't convert string to int to do the increment.
Also `print("You are {} years old going on {}".format(age,age+1)) for completeness.
Just to add, try to stick with the second print format, because that way you are in charge of formatting your print statement properly. Better, experiment with different format like %f, %s, %r and other. Cheers
1

In all print cases you're trying to add a str to an int and the error tells you that that form of implicit convesion is not possible:

'21' +1 TypeErrorTraceback (most recent call last) <ipython-input-60-3473188b220d> in <module>() ----> 1 '21' +1 TypeError: Can't convert 'int' object to str implicitly 

input behaves differently in Python 3.x in that it doesn't evaluate the input but just returns it as a str.

Wrap the input result in an int call to get it to work by explicitly casting the string to an int:

age = int(input("How old are you?")) 

The only caveat here is if you don't supply a value during input that's capable of being transformed to an int you'll get a ValueError. In this case you should create a while loop that will try and transform input to int and break on success (i.e no Exception raised).

2 Comments

Can I say that Python does not do any auto parsing from int to string like Java? Hence I always have to perform an explicit parsing?
@user3437460 yes, you have to be explicit in cases like input.
0

There is no need to change it into int it is already.

>>> age = input("age") age21 >>> age 21 >>> type(age) <type 'int'> 

As you are concatenating string and integer you are not getting the result


Try This:

print("You are {} years old going on {}".format(age,age+1)) 

3 Comments

I still need to do age = int(input("How old are you?")) first.
In my case, it shows '21' instead of 21. In my case age is still of type str.
@user3437460 That is a difference between Python 2 and 3. In Python 3: input is the equivalent of Python 2 raw_input - they return strings. However, input in Python 2 is the same as doing eval(input()) in Python 3.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.