Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new line with variable in python

When I use "\n" in my print function it gives me a syntax error in the following code

from itertools import combinations
a=[comb for comb in combinations(range(1,96+1),7) if sum(comb) == 42]
print (a "\n")

Is there any way to add new line in each combination?

like image 833
user6234753 Avatar asked Jan 07 '23 05:01

user6234753


1 Answers

The print function already adds a newline for you, so if you just want to print followed by a newline, do (parens mandatory since this is Python 3):

print(a)

If the goal is to print the elements of a each separated by newlines, you can either loop explicitly:

for x in a:
    print(x)

or abuse star unpacking to do it as a single statement, using sep to split outputs to different lines:

print(*a, sep="\n")

If you want a blank line between outputs, not just a line break, add end="\n\n" to the first two, or change sep to sep="\n\n" for the final option.

like image 178
ShadowRanger Avatar answered Jan 18 '23 18:01

ShadowRanger