如何在 Python 中的字符串中打印递增的 int 标识符
How to print an incremented int identifier in a string in Python
在Java中,如果我想打印一个递增的int变量,我可以这样做:
int age = scn.nextInt();
System.out.println("You are " + age + " years old going on " + (age+1));
输出:
21
You are 21 years old going on 22
是否可以在 Python 中执行相同的操作?
我尝试了以下方法,none 有效。
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))
我已经尝试过这些链接中提供的解决方案:
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
您需要将输入转换为 int :
>>> age = int(input("How old are you?"))
然后是以下工作:
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))
在所有 print
情况下,您尝试将 str
添加到 int
并且错误告诉您这种形式的隐式转换是不可能的:
'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
在 Python 3.x
中的行为不同,因为它不评估输入,而只是 returns 它作为 str
.
将输入结果包装在 int
调用中,通过将字符串显式转换为 int
:
使其工作
age = int(input("How old are you?"))
此处唯一需要注意的是,如果您在输入期间未提供可以转换为 int
的值,您将得到 ValueError
。在这种情况下,您应该创建一个 while
循环,它将 try
并将输入转换为 int
和 break
成功(即没有引发异常)。
不需要改成int
已经是
>>> age = input("age")
age21
>>> age
21
>>> type(age)
<type 'int'>
当您连接字符串和整数时,您没有得到结果
试试这个:
print("You are {} years old going on {}".format(age,age+1))
在Java中,如果我想打印一个递增的int变量,我可以这样做:
int age = scn.nextInt();
System.out.println("You are " + age + " years old going on " + (age+1));
输出:
21
You are 21 years old going on 22
是否可以在 Python 中执行相同的操作?
我尝试了以下方法,none 有效。
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))
我已经尝试过这些链接中提供的解决方案:
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
您需要将输入转换为 int :
>>> age = int(input("How old are you?"))
然后是以下工作:
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))
在所有 print
情况下,您尝试将 str
添加到 int
并且错误告诉您这种形式的隐式转换是不可能的:
'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
在 Python 3.x
中的行为不同,因为它不评估输入,而只是 returns 它作为 str
.
将输入结果包装在 int
调用中,通过将字符串显式转换为 int
:
age = int(input("How old are you?"))
此处唯一需要注意的是,如果您在输入期间未提供可以转换为 int
的值,您将得到 ValueError
。在这种情况下,您应该创建一个 while
循环,它将 try
并将输入转换为 int
和 break
成功(即没有引发异常)。
不需要改成int
已经是
>>> age = input("age")
age21
>>> age
21
>>> type(age)
<type 'int'>
当您连接字符串和整数时,您没有得到结果
试试这个:
print("You are {} years old going on {}".format(age,age+1))