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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • input gives you a string. You have to convert it to an integer before you add 1: `age = int(input...` – raphv Aug 10 '16 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.** – user3437460 Aug 10 '16 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](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) or produce a [mre]. – Karl Knechtel Jul 27 '22 at 03:39

3 Answers3

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))
3kt
  • 2,543
  • 1
  • 17
  • 29
  • 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. – user3437460 Aug 10 '16 at 10:27
  • 1
    Also `print("You are {} years old going on {}".format(age,age+1)) for completeness. – Gábor Fekete Aug 10 '16 at 11:05
  • 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 – Just Ice Aug 10 '16 at 15:41
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).

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
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))
bhansa
  • 7,282
  • 3
  • 30
  • 55
  • I still need to do `age = int(input("How old are you?"))` first. – user3437460 Aug 10 '16 at 10:29
  • In my case, it shows `'21'` instead of `21`. In my case `age` is still of type `str`. – user3437460 Aug 10 '16 at 10:43
  • 1
    @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. – juanpa.arrivillaga Aug 10 '16 at 10:50