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

0

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

Note: this method using fstring is only available for python > 3.6.
That is right. @Marcel, you can also use print('vector {0}'.format(i)).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.