Python print new line with variable

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?

ShadowRanger

131k12 gold badges168 silver badges244 bronze badges

asked Apr 21, 2016 at 21:07

10

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.

answered Apr 21, 2016 at 21:57

ShadowRangerShadowRanger

131k12 gold badges168 silver badges244 bronze badges

Two possibilities:

print "%s\n" %a
print a, "\n"

answered Apr 21, 2016 at 21:12

PfunnyGuyPfunnyGuy

6758 silver badges20 bronze badges

0

This will work for you:

I used 1,2...6 in my example and 2 length tuples with a combination sum of 7.

from itertools import combinations
a=["{0}\n".format[comb] for comb in combinations[range[1,7],2] if sum[comb] == 7]

print[a]
for thing in a:
    print[thing]

Output

['[1, 6]\n', '[2, 5]\n', '[3, 4]\n']
[1, 6]

[2, 5]

[3, 4]

answered Apr 21, 2016 at 21:35

Garrett RGarrett R

2,64910 silver badges15 bronze badges

for me in the past something like print["\n",a] works.

answered Jan 3, 2020 at 23:37

fastlanfastlan

611 silver badge5 bronze badges

Print new line after a variable in Python #

Use the addition operator to print a new line after a variable, e.g. print[variable + '\n']. The newline [\n] character is a special character in python and is used to insert new lines in a string.

Copied!

# ✅ Print new line after variable variable = "bobby" my_str = variable + '\n' + 'hadz' # bobby # hadz print[my_str] # ---------------------------------- # ✅ Print new line after each item in list of strings # 👇️ print items of list of strings on separate lines my_list = ['bobby', 'hadz', 'com'] result = '\n'.join[my_list] # bobby # hadz # com print[result] # ---------------------------------- # ✅ Print new line after each item in list of integers my_list = [2, 4, 8] result = '\n'.join[str[num] for num in my_list] # 2 # 4 # 8 print[result]

The first example uses the addition [+] operator to print a new line after a variable.

Copied!

variable = "bobby" my_str = variable + '\n' + 'hadz' # bobby # hadz print[my_str]

If the variable is not of type string, pass it to the str[] class before using the addition operator.

Alternatively, you can use a formatted string literal.

Copied!

variable = "bobby" my_str = f'{variable}\nhadz' # bobby # hadz print[my_str]

Formatted string literals [f-strings] let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

When using a formatted string literal, you don't have to convert the value stored in the variable to a string as this is done for us automatically.

If you need to print a new line after each item in a list, use the str.join[] method to join the list with a newline [\n] character separator.

Copied!

my_list = ['bobby', 'hadz', 'com'] result = '\n'.join[my_list] # bobby # hadz # com print[result]

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your iterable contains numbers or other types, convert all of the values to strings before calling join[].

Copied!

my_list = [2, 4, 8] result = '\n'.join[str[num] for num in my_list] # 2 # 4 # 8 print[result]

The string the method is called on is used as the separator between the elements.

Alternatively, you can use a for loop to iterate over the list and print each item.

Copied!

my_list = ['bobby', 'hadz', 'com'] for item in my_list: # bobby # hadz # com print[item]

By default, the print[] function prints a newline character at the end of each message.

You can change this behavior by setting the end argument in the call to print[].

Copied!

print['a', 'b', 'c'] # 👉️ 'a b c\n' print['a', 'b', 'c', end=''] # 👉️ 'a b c'

How do you print a new line after a variable in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = , which is the character that will be used to separate the lines.

How do you print a new line variable?

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.

How do you print a variable and string together in Python?

How to print a variable and a string in Python by separating each with a comma. You can print text alongside a variable, separated by commas, in one print statement. In the example above, I first included some text I wanted to print in double quotation marks – in this case, the text was the string Hello .

How do you print a line break in Python?

Use "\n" to print a line break Insert "\n" at the desired line break position.

Chủ Đề