This print("{0:10d}".format(5)) will print 5 after 9 blanks.
For more reference on formatting in python refer this.
This print("{0:10d}".format(5)) will print 5 after 9 blanks.
For more reference on formatting in python refer this.
In general case, we could use string .format():
>>> '{:<30}'.format('left aligned')
'left aligned '
>>> '{:>30}'.format('right aligned')
' right aligned'
>>> '{:^30}'.format('centered')
' centered '
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********'
We could also do that with f strings for python >3.6
>>> f"{'left aligned':<30}"
'left aligned '
>>> f"{'right aligned':>30}"
' right aligned'
>>> f"{'centered':^30}"
' centered '
>>> f"{'centered':*^30}" # use '*' as a fill char
'***********centered***********'
How to print spaces in Python? - Stack Overflow
python 3.x - Print numbers with spaces in Python3 - Stack Overflow
string - Python - Printing numbers with spacing format - Stack Overflow
How do I add space between two variables after a print in Python - Stack Overflow
Here's a short answer
x=' '
This will print one white space
print(x)
This will print 10 white spaces
print(10*x)
Print 10 whites spaces between Hello and World
print(f"Hello{x*10}World")
If you need to separate certain elements with spaces you could do something like
print "hello", "there"
Notice the comma between "hello" and "there".
If you want to print a new line (i.e. \n) you could just use print without any arguments.
str.center can make things easier.
for i in list:
print '[ ' + ' | '.join([str(j).center(4) for j in i]) + ' ]'
Output:
[ 4 | 3 | 7 | 23 ]
[ 17 | 4021 | 4 | 92 ]
In case you need an alternative solution, you can use str.format:
for i in list:
print '[ ' + ' | '.join(["{:^4}".format(j) for j in i]) + ' ]'
Output:
[ 4 | 3 | 7 | 23 ]
[ 17 | 4021 | 4 | 92 ]
You can also use third-parties like PrettyTable or texttable. Example using texttable:
import texttable
l = [(4, 3, 7, 23),(17, 4021, 4, 92)]
table = texttable.Texttable()
# table.set_chars(["", "|", "", ""])
table.add_rows(l)
print(table.draw())
Would produce:
+----+------+---+----+
| 4 | 3 | 7 | 23 |
+====+======+===+====+
| 17 | 4021 | 4 | 92 |
+----+------+---+----+
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)
Use string interpolation instead.
print '%d %f' % (count,conv)
Here is bad but simple solution if you don't want to mess with locale:
'{:,}'.format(1234567890.001).replace(',', ' ')
Answer of @user136036 is quite good, but unfortunately it does not take into account reality of Python bugs. Full answer could be following:
Variant A
If locale of your platform is working right, then just use locale:
import locale
locale.setlocale(locale.LC_ALL, '')
print("{:,d}".format(7123001))
Result is dependent on your locale and Python implementation working right.
But what if Python formatting according to locale is broken, e.g. Python 3.5 on Linux?
Variant B
If Python does not respect grouping=True parameter, you can use locale and a workaround (use monetary format):
locale.setlocale(locale.LC_ALL, '')
locale._override_localeconv = {'mon_thousands_sep': '.'}
print(locale.format('%.2f', 12345.678, grouping=True, monetary=True))
Above gives 12.345,68 on my platform. Setting monetary to False or omitting it - Python does not group thousands.
Specifying locale._override_localeconv = {'thousands_sep': '.'} do nothing.
Variant C
If you don't have time to check what is working OK and what is broken with Python on your platform, you can just use regular string replace function (if you want to swap commas and dot to dots and comma):
print("{:,.2f}".format(7123001.345).replace(",", "X").replace(".", ",").replace("X", "."))
Replacing comma for space is trivial (point is assumed decimal separator):
print("{:,.2f}".format(7123001.345).replace(",", " ")
You can use rjust:
'hi'.rjust(3)
' hi'
print(' '.join(str(e).rjust(2) for e in mylist))
You can use string formatting:
print(" ".join("{:2d}".format(e) for e in mylist))
Also, " ".join can be replaced by unpacking due to the default sep=' ' of the print function:
print(*("{:2d}".format(e) for e in mylist))
You can apply the list as separate arguments:
print(*L)
and let print() take care of converting each element to a string. You can, as always, control the separator by setting the sep keyword argument:
>>> L = [1, 2, 3, 4, 5]
>>> print(*L)
1 2 3 4 5
>>> print(*L, sep=', ')
1, 2, 3, 4, 5
>>> print(*L, sep=' -> ')
1 -> 2 -> 3 -> 4 -> 5
Unless you need the joined string for something else, this is the easiest method. Otherwise, use str.join():
joined_string = ' '.join([str(v) for v in L])
print(joined_string)
# do other things with joined_string
Note that this requires manual conversion to strings for any non-string values in L!
Although the accepted answer is absolutely clear, I just wanted to check efficiency in terms of time.
The best way is to print joined string of numbers converted to strings.
print(" ".join(list(map(str,l))))
Note that I used map instead of loop. I wrote a little code of all 4 different ways to compare time:
import time as t
a, b = 10, 210000
l = list(range(a, b))
tic = t.time()
for i in l:
print(i, end=" ")
print()
tac = t.time()
t1 = (tac - tic) * 1000
print(*l)
toe = t.time()
t2 = (toe - tac) * 1000
print(" ".join([str(i) for i in l]))
joe = t.time()
t3 = (joe - toe) * 1000
print(" ".join(list(map(str, l))))
toy = t.time()
t4 = (toy - joe) * 1000
print("Time",t1,t2,t3,t4)
Result:
Time 74344.76 71790.83 196.99 153.99
The output was quite surprising to me. Huge difference of time in cases of 'loop method' and 'joined-string method'.
Conclusion: Do not use loops for printing list if size is too large( in order of 10**5 or more).