-2

If I have five variables - each named var0, var1, var2, var3, var4 is there a shorter way to print these than typing out print(var0), print(var1) etc. for each one? Perhaps a for loop? Somewhat new to python. I have something I'm trying to create. If this is possible it should get me where I need to be.

Edit: They are dynamic variables, so none of those solutions would work. It could be var1-5 or var1-25.

Thomas Colbert
  • 152
  • 1
  • 15
  • 1
    Yep there is: `print(var1, var2, var3, var4, sep='\n')`. – Christian Dean Jan 16 '17 at 22:59
  • Have you heard of lists? – jwodder Jan 16 '17 at 22:59
  • @leaf please do not post answers as comments. – Moon Cheesez Jan 16 '17 at 23:00
  • Wouldn't it be better to have those variables in a list? In which case to print them out in a single liner would be trivial, as well as lots of other things, probably. – xzoert Jan 16 '17 at 23:00
  • 4
    @MoonCheesez it is absolutely ok to post an answer as a comment – Sterling Archer Jan 16 '17 at 23:00
  • If they all the same kind of thing (homogeneous), keep them in a list. If they all different kinds of things (heterogeneous), keep them in a namedtuple. – Peter Wood Jan 16 '17 at 23:02
  • 2
    Possible duplicate of [Print multiple arguments in python](http://stackoverflow.com/questions/15286401/print-multiple-arguments-in-python) – TemporalWolf Jan 16 '17 at 23:02
  • Well, they're dynamic variables, so it could be 1-5 or 1-10 or however many so that wouldn't work, and i start with a list that I'm splitting into separate strings. – Thomas Colbert Jan 16 '17 at 23:02
  • @MoonCheesez Why not? I really don't think my previous comment would work well as an answer. – Christian Dean Jan 16 '17 at 23:07
  • @leaf It is a common problem that questions are resolved in the comments but the question does not have an answer. (http://meta.stackoverflow.com/questions/253045/answerers-who-only-use-comments and http://meta.stackoverflow.com/questions/251597/question-with-no-answers-but-issue-solved-in-the-comments) however, it could be the case where you did not have time to create a full answer: http://meta.stackexchange.com/questions/4217/why-do-some-people-answer-in-comments – Moon Cheesez Jan 16 '17 at 23:10

4 Answers4

1

If you have a list of values you can join them together with a separator.

>>> print('\n'.join(['a', 'b', 'c', 'd'])
a
b
c
d
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
1

The new print() function in Python 3.x takes a sep keyword argument which is used to separate the contents passed to print(). This can be used to separate the output of each of your variables by newlines - as if they were being printed separately:

>>> var1, var2, var3, var4 = 1, 2, 3, 4
>>> print(var1, var2, var3, var4, sep='\n')
1
2
3
4
>>>
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

Just do print(var1, var2, var3, var 4, sep='\n'). It should print them out with a blank line inbetween.

Rob.S
  • 23
  • 1
  • 4
0

This command will work without change, even if you have var1 up to var99999.

import re
print(["{} = {}".format(v, eval(v)) for v in dir() if re.match('var.*', v)])
Hkoof
  • 756
  • 5
  • 14