[python] How do I add space between two variables after a print in Python

I'm fairly new to Python, so I'm trying my hand at some simple code. However, in one of the practices my code is supposed to display some numbers in inches on the left and the conversion of the numbers on the right;

count = 1
conv = count * 2.54
print count, conv

I want the output to be printed with some space between them;

count = 1
conv = count * 2.54
print count,     conv

I can't figure out how to do this. I've searched everywhere, but all I can find are people trying to get rid of space. If someone could just lead me in the right direction, I'd be thankful.

Oh, and I just realized that I'm using Python 2.7, not 3.x. Not sure if this is important.

This question is related to python variables printing space punctuation

The answer is


A quick warning, this a pretty wordy answer.

print is tricky sometimes, I had some problems with it when I first started. What you want is a few spaces in between two variables after you print them right? There's many ways to do this, as shown in the above answers.

This is your code:

count = 1
conv = count * 2.54
print count, conv

It's output is this:

1 2.54

If you want spaces in between, you can do it the naive way by sticking a string of spaces in between them. The variables count and conv need to be converted to string types to concatenate(join) them together. This is done with str().

print (str(count) + "           " + str(conv))
### Provides an output of:
1           2.54

To do this is the newer, more pythonic way, we use the % sign in conjunction with a letter to denote the kind of value we're using. Here I use underscores instead of spaces to show how many there are. The modulo before the last values just tells python to insert the following values in, in the order we provided.

print ('%i____%s' % (count, conv))
### provides an output of:
1____2.54

I used %i for count because it is a whole number, and %s for conv, because using %i in that instance would provide us with "2" instead of "2.54" Technically, I could've used both %s, but it's all good.

I hope this helps!

-Joseph

P.S. if you want to get complicated with your formatting, you should look at prettyprint for large amounts of text such as dictionaries and tuple lists(imported as pprint) as well as which does automatic tabs, spacing and other cool junk.

Here's some more information about strings in the python docs. http://docs.python.org/library/string.html#module-string


This is a stupid/hacky way

print count,    
print conv

print str(count) + ' ' + str(conv) - This did not work. However, replacing + with , works for me


A simple way to add the tab would be to use the \t tag.

print '{0} \t {1}'.format(count, conv)

Alternatively you can use ljust/rjust to make the formatting nicer.

print "%s%s" % (str(count).rjust(10), conv)

or

print str(count).ljust(10), conv

Use string interpolation instead.

print '%d   %f' % (count,conv)

print( "hello " +k+ "  " +ln);

where k and ln are variables


You can do it this way in python3:

print(a,b,end=" ")


You should use python Explicit Conversion Flag PEP-3101

'My name is {0!s:10} {1}'.format('Dunkin', 'Donuts')

'My name is Dunkin Donuts'

or

'My name is %-10s %s' % ('Dunkin', 'Donuts')

'My name is Dunkin Donuts'

https://www.python.org/dev/peps/pep-3101/


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to variables

When to create variables (memory management) How to print a Groovy variable in Jenkins? What does ${} (dollar sign and curly braces) mean in a string in Javascript? How to access global variables How to initialize a variable of date type in java? How to define a variable in a Dockerfile? Why does foo = filter(...) return a <filter object>, not a list? How can I pass variable to ansible playbook in the command line? How do I use this JavaScript variable in HTML? Static vs class functions/variables in Swift classes?

Examples related to printing

How do I print colored output with Python 3? Print a div content using Jquery Python 3 print without parenthesis How to find integer array size in java Differences Between vbLf, vbCrLf & vbCr Constants Printing variables in Python 3.4 Show DataFrame as table in iPython Notebook Removing display of row names from data frame Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window Print a div using javascript in angularJS single page application

Examples related to space

How to create string with multiple spaces in JavaScript Removing body margin in CSS What is the symbol for whitespace in C? Using tr to replace newline with space CSS: how to add white space before element's content? Regular expression to allow spaces between words UL has margin on the left EXCEL VBA Check if entry is empty or not 'space' How do I make Java register a string input with spaces? Handling of non breaking space: <p>&nbsp;</p> vs. <p> </p>

Examples related to punctuation

How do I add space between two variables after a print in Python Best way to strip punctuation from a string