Python - Whitespace after

0 votes
Why is there a white space that outputs when I print something like this in Python 3? Is it in the '\n' character itself?

print (my_var1, '\n', my_var_2)
Output :

1
 2
 

I know it can be fixed but why do we get the space usually?
May 30 in Python by ana1504.k
• 7,870 points
13 views

1 answer to this question.

0 votes

print adds a single space after every argument, including `\n'. You might want to combine the three strings into a single argument yourself.

print(my_var1 + '\n' + my_var2)

or

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

Better than either of these would be to use the format string method:

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

which both handles conversion to str if necessary and eliminates any temporary objects.

answered May 30 by SDeb
• 13,160 points

Related Questions In Python

0 votes
1 answer

Python Whitespace for indenting

PEP-8 recommends creating indents by tapping the ...READ MORE

answered Jun 12, 2018 in Python by Hamartia's Mask
• 1,580 points
31 views
+1 vote
2 answers

Remove all whitespace in a string in Python

You can also use regular expressions for ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 67,290 points
1,282 views
0 votes
1 answer

Can't Click an Element in Python Selenium After Successfully Finding It

I've encountered this problem of not being ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 56,520 points
1,094 views
0 votes
1 answer

Can't Click an Element in Python Selenium After Successfully Finding It

I've encountered this problem of not being ...READ MORE

answered Oct 8, 2018 in Python by Priyaj
• 56,520 points
87 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 56,520 points
288 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 14, 2018 in Python by Priyaj
• 56,520 points
33 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,520 points
44 views
+1 vote
1 answer

How is raw_input() and input() in python3.x?

raw_input() was renamed to input() so now input() returns the exact string ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 56,520 points
37 views
0 votes
1 answer

Python sort() function arguments

Both sort and sorted have three keyword arguments: cmp, key and reverse. L.sort(cmp=None, key=None, reverse=False) -- ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,160 points
227 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,160 points
32 views