Python - space after

Why is there an empty space that is displayed when I print something like this in Python 3? (Is this a symbol '\n'?)

print (my_var1, '\n', my_var_2)

Output:

1
 2

I know how to fix it. It's not that difficult, but I'm just wondering why ...

+4
source share
2 answers

printadds one space (or keyword argument value sep) after each argument, including `\ n '. You may want to combine three lines into one argument yourself.

print(my_var1 + '\n' + my_var2)

or

print('\n'.join([my_var1, my_var2]))

Better than any of theses will use the string method format:

print('{}\n{}'.format(my_var1, my_var2))

str .

, , sep \n, @billy.

+8

print, str sep - (' '). .

print(my_var1)
print(my_var2)

for var in (my_var1, my_var2):
    print(var)

print(my_var1, my_var2, sep='\n')

.

+5

Source: https://habr.com/ru/post/1679053/


All Articles