For循环在Python中打印变量

For循环在Python中打印变量,python,python-3.x,Python,Python 3.x,我想在Python3中打印变量vector1和vector2,而不必手动编写打印代码。我该怎么做?下面您可以看到我尝试使用的代码 vectorInput = input("Enter vectors values separated by ',' and vectors separated by ' ': ") vector1,vector2 = vectorInput.split(" ") for num in range(1,3): print({}.format('vector

我想在Python3中打印变量vector1和vector2,而不必手动编写打印代码。我该怎么做?下面您可以看到我尝试使用的代码

vectorInput = input("Enter vectors values separated by ',' and vectors separated by ' ': ")

vector1,vector2 = vectorInput.split(" ")

for num in range(1,3):
    print({}.format('vector'+num))

谢谢。

嗯,你可以直接使用理解

[print(i) for i in vectorInput.split(" ")]
或者使用向量的
列表
,因为它更适合您的使用模式,您可以稍后重用它

vectors = vectorInput.split(" ")
[print(i) for i in vectors]
或使用
表示

vectors = vectorInput.split(" ")
for i in vectors:
    print(i)

这是较短的版本,请试一试

for i in input("Enter vectors values separated by ',' and vectors separated by ' ': ").split():
    print(f'vector {i}') 

如果希望i是一个整数,那么将i替换为
int(i)

注意:这种使用的方法仅适用于python>3.6。这是正确的@Marcel,您也可以使用print('vector{0}'。format(i))。