-1

I want to print variables vector1 and vector2 in Python 3, without having to write the print code manually. How can I do this? Below you can see the code that I tried using for this.

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))

Thank you.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
Marcel
  • 1
  • 1
  • 2

2 Answers2

0

Well, you can use comprehensions directly.

[print(i) for i in vectorInput.split(" ")]

Or use list of vectors, as it more fits in your usage pattern, and ou can reuse it later.

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

Or with for

vectors = vectorInput.split(" ")
for i in vectors:
    print(i)
Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
0

This is the shorter version give a try.

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

If you want i as an integer then replace i with int(i)

Rarblack
  • 4,559
  • 4
  • 22
  • 33