Delete space between two print

0

How can I eliminate the space between 2 print different in Python? For example:

num = raw_input()
num = float(num)
result=""
while num>=1000:
    result+= "M"
    num = num - 1000
if num>=900:
    result+= "CM",
    num=num-900
if num>=500:
    result+= "D",
    num=num-500
if num>=400:
    result+= "CD",
    num=num-400
while num>=100:
    result+= "C",
    num=num-100
if num>=90:
    result+= "XC",
    num=num-90
if num>=50:
    result+= "L",
    num=num-50
if num>=40:
    result+= "XL",
    num=num-40
while num>=10:
    result+= "X",
    num=num-10
if num == 9:
    result+= "IX",
    num=num-9
if num>=5:
    result+= "V",
    num=num-5
if num>=4:
    result+= "IV",
    num=num-4
while num>0:
    result+= "I",
    num=num-1  

print result

Print it to me like this:

  

D C

but I want it this way:

  

DC

  

NOTE: If I take out the comma at the end, it returns them one below the other.

    
asked by Miled 16.05.2018 в 23:49
source

3 answers

0

One option is to concatenate the result in a variable result type string, so you can build the string as you wish (insert any symbol or space).

num = raw_input()
num = float(num)
result=""
while num>=1000:
    result+= "M"
    num = num - 1000
if num>=900:
    result+= "CM"
    num=num-900
if num>=500:
    result+= "D"
    num=num-500
if num>=400:
    result+= "CD"
    num=num-400
while num>=100:
    result+= "C"
    num=num-100
if num>=90:
    result+= "XC"
    num=num-90
if num>=50:
    result+= "L"
    num=num-50
if num>=40:
    result+= "XL"
    num=num-40
while num>=10:
    result+= "X"
    num=num-10
if num == 9:
    result+= "IX"
    num=num-9
if num>=5:
    result+= "V"
    num=num-5
if num>=4:
    result+= "IV"
    num=num-4
while num>0:
    result+= "I"
    num=num-1  

print result

In case it enters both ifs, then the output would be like this:

  

CMD

    
answered by 17.05.2018 / 00:43
source
1

You could try the end command that allows you to vary the impressions of your code like this:

from __future__ import print_function
num=2000
if num>=900:
    print ('CM',end='')
    num=num-900
if num>=500:
    print ("D")
    num=num-500

output:
CMD
  

Note the from __future__ import print_function is so that it does not mark syntax error since this function works in python 3.7

    
answered by 17.05.2018 в 00:31
0

You have several options:

  • Use sys.stdout instead of print :

    import sys
    
    sys.stdout.write("D")
    sys.stdout.write("C")
    
  • Go to Python 3 or use% co_of% of it in Python 2 .

    This is because in Python 3 print is a function and has the parameters print (character to be added at the end of end , new line by default) and print (character to add between objects to be printed within a same call of sep ) that allow you to do just what you want :

    from __future__ import print_function
    
    print("D", end='')
    print("C", end='')
    

If you want to print a series of objects with a separator of your own or without any of them, but using only print you can also use string formatting or print for example:

>>> letras = ("H", "o", "l", "a")
>>> print "".join(letras)
Hola
>>> print "{}{}{}{}".format(*letras)
Hola

>>> hora = "2"
>>> min = "15"
>>> sec = "58"
>>> print '{}:{}:{}'.format(hora, min, sec)
2:15:58
>>> print '%d:%d:%d' % (hora, min, sec)
2:15:58
    
answered by 17.05.2018 в 00:31