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)
Answer from Óscar López on Stack OverflowA 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)
How to format output to create space between two variables in python when printing? - Stack Overflow
How to print spaces in Python? - Stack Overflow
python - Format a string with a space between every two digits - Stack Overflow
python - How to print spaces between values in loop? - Stack Overflow
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***********'
Give this a try:
between = ' '*4
print('Kilograms{between}Pounds'.format(between=between))
for kg in range(199):
kg += 1
lb = 2.2
lb = kg * lb
lb = round(lb, 2)
print('{kg:<{kg_width}}{between}{lb:>{lb_width}}'.format(
kg=kg, kg_width=len('Kilograms'),
between=between,
lb=lb, lb_width=len('Pounds')))
# Output:
# Kilograms Pounds
# 1 2.2
# 2 4.4
# 3 6.6
# 4 8.8
# 5 11.0
# 6 13.2
# 7 15.4
# 8 17.6
# 9 19.8
# 10 22.0
# 11 24.2
# ...
The big gnarly print is just because I tried to parameterize everything. Given the fixed column names and spacing, you could just do this:
print('{kg:<9} {lb:>6}'.format(kg=kg, lb=lb))
EDIT
Closer to your original code:
print("Kilograms Pounds")
for kg in range(0, 199):
kg += 1
lb = 2.2
lb = kg * lb
lb = round(lb, 2)
print(format(kg, "<4d"), end = '')
print(" ", end = '')
print(format(lb, ">7.1f"))
Check out The Docs Section 7.1.3.1
You can pass format() a width as int, which should take care of your whitespace problem.
From the Documentation Example:
>>> for num in range(5,12):
for base in 'dXob':
print('{0:{width}{base}}'.format(num, base=base, width=width),end=' ')
produces:
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
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.
You could use something like:
s = "534349511"
print ' '.join([s[i:i+2] for i in range(0,len(s),2)])
Note that this will work for lists of uneven length as well -- you'll just have a single digit at the end, after a space.
Try this:
for i in xrange(0, len(input), 2):
out += input[i:i+2] + " "
While others have given an answer, a good option here is to avoid using a loop and multiple print statements at all, and simply use the * operator to unpack your iterable into the arguments for print:
>>> print(*range(5))
0 1 2 3 4
As print() adds spaces between arguments automatically, this makes for a really concise and readable way to do this, without a loop.
>>> for i in range(5):
... print(i, end=' ')
...
0 1 2 3 4
Explanation: the sep parameter only affects the seperation of multiple values in one print statement. But here, you have multiple print statements with one value each, so you have to specify end to be a space (per default it's newline).
Having this stupidly small issue I can't seem to figure out. The code is
favorite_color = input('Enter favorite color:\n')
word1 = input('Enter a word:\n')
num1 = int(input('Enter an integer:\n'))
str_list = [favorite_color, word1]
password1 = '_'.join(str_list)
print('\nFirst password:', password1)
print(f'\nSecond password: {num1}{word1}{num1}')I want the bottom 2 output lines to print with a space between them, but they both output right on top of each other. Any answers are much appreciated!