How to add space between two variables after printing in Python

I am new to Python, so I am trying to use my simple code. However, in one of the methods, my code should display some numbers in inches to the left and convert numbers to the right;

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

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

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

I can’t figure out how to do this. I 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 would be grateful.

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

+8
source share
8 answers

A simple way would be:

 print str(count) + ' ' + str(conv) 

If you need more spaces, just add them to the line:

 print str(count) + ' ' + str(conv) 

Better using the new syntax to format strings:

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

Or using the old syntax, limiting the number of decimal places to two:

 print '%d %.2f' % (count, conv) 
+8
source

Use the interpolation string .

 print '%d %f' % (count,conv) 
+4
source

Alternatively, you can use ljust / rjust to make formatting more enjoyable.

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

or

 print str(count).ljust(10), conv 
+3
source

A quick warning, this is a pretty verbose answer.

typing is sometimes difficult, I had problems with this when I first started. What do you want is a few spaces between two variables after you type them correctly? There are many ways to do this, as shown in the answers above.

This is your code:

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

It outputs the following:

 1 2.54 

If you need gaps between them, you can do it in a naive way by inserting a space between them. The variables count and conv must be converted to string types in order to concatenate them (concatenate). This is done using str ().

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

To do this, this is a newer, more pythonic way, we use the% sign in combination with a letter to indicate the type of value that we use. Here I use underscores instead of spaces to show how many there are. Modulo before the last values ​​simply tells python to insert the following values ​​in the order in which we specified.

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

I used% i for count, because it is an integer, and% s for conv, because using% i in this case will give us "2" instead of "2.54". Technically, I could use as% s, but all this is good.

Hope this helps!

Joseph

PS if you want to complicate your formatting, you should look at attractive text for a lot of text, for example, dictionaries and lists of tuples (imported as pprint), as well as which makes automatic tabs, spaces and other cold garbage.

Here's more info on the lines in python docs. http://docs.python.org/library/string.html#module-string

+2
source

This is a stupid / hacker way

 print count, print conv 
+1
source

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

0
source

You must 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/

0
source

An easy way to add a tab is to use the \t tag.

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

All Articles