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

Alexander Huszagh
  • 12,117
  • 3
  • 36
  • 66
Marcel
  • 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,229
  • 2
  • 20
  • 31
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,396
  • 4
  • 20
  • 32
  • 1
    Note: this method using [fstring](https://www.python.org/dev/peps/pep-0498/) is only available for python > 3.6. – Buky Oct 18 '18 at 12:30
  • That is right. @Marcel, you can also use print('vector {0}'.format(i)). – Rarblack Oct 18 '18 at 12:48