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
• 6,670 points
9 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 10,780 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
22 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
• 66,880 points
717 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,140 points
741 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,140 points
71 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,140 points
166 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,140 points
24 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,140 points
37 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,140 points
30 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
• 10,780 points
165 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
• 10,780 points
28 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.