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

153,548

Solution 1

A simple way would be:

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

If you need more spaces, simply add them to the string:

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

A fancier way, using the new syntax for string formatting:

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

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

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

Solution 2

You can do it this way in Python 3:

print(a, b, sep=" ")

Solution 3

Use string interpolation instead.

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

Solution 4

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

Solution 5

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

Share:
153,548
Hebon
Author by

Hebon

Updated on July 09, 2022

Comments

  • Hebon
    Hebon almost 2 years

    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.

  • Hebon
    Hebon about 12 years
    Neither of those work. The first way gives me a Traceback error, and the second one acts as though it's print count, conv
  • Hebon
    Hebon about 12 years
    That almost works, but now I have extra zeros at the end of my "conv" variable Inches Centimeters 1 2.540000
  • Óscar López
    Óscar López about 12 years
    Fixed the first one, for all of them: you can add as many spaces as you need in between!
  • Whatang
    Whatang about 12 years
    The first won't work because you can't add an integer to a string, nor a string to a float. To get more space in the second and third examples, just put the required number of spaces between the {0} and the {1} or the %d and %f.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 12 years
    So then tune the %f specifier.
  • Óscar López
    Óscar López about 12 years
    @Hebon fixed all the examples with two spaces in-between. It's very simple to add the spaces you need, just add them in the middle. Also, fixed the last example for displaying only two decimals
  • Hebon
    Hebon about 12 years
    I managed to eliminate the Traceback by getting rid of some unused code, however, it's still not working. Using print str(count) + ' ' + str(conv) it only mashes them together so that they appear as "12.54".
  • Óscar López
    Óscar López about 12 years
    Are you sure that you're typing the correct number of spaces in-between? it works, all of them work, you can be sure of it.
  • Hebon
    Hebon about 12 years
    @ÓscarLópez I think I got it to work. I used print '{0} {1}'.format(count, conv). I believe the issue I was having was some unused code at the begining. When I removed it, it stopped the Traceback issues I was getting, and allowed your suggestion to work. That in itself confuses me though, since all the code did was ask for some unused inputs.
  • Karl Knechtel
    Karl Knechtel about 12 years
    "Traceback" is not a part of the name of the error. It does not describe the error. Rather, the traceback is the text that is displayed to tell you what happened when the error occurred (it will show you the line of code that failed, and the lines that got you to that point). The error type is on the line that says "<some kind of error>: <some explanation>", like TypeError: unsupported operand type(s) for +: 'int' and 'str'. That is a TypeError, as it says.
  • Li-aung Yip
    Li-aung Yip about 12 years
    I actually like this solution more than the printf relatives. It's easier to understand than a printf format specifier and does exactly what it says on the tin.
  • Hebon
    Hebon about 12 years
    This is very informative. Thank you for taking the time to explain it.
  • Joseph Daniels
    Joseph Daniels about 12 years
    Thanks, I'm happy I could be helpful!