How to print an incremented int identifier in a string in Python
Question:
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
Answers:
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))
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).
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))
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
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))
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).
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))