-2

After executing the code I get the following print out:

"Het antwoord van de berekening is: 8775 ."

When I want to get "Het antwoord van de berekening is: 8775.". So I want to remove the space between the number and the dot. How do I do that?

Berekening1 = 8.5
Berekening2 = 8.1+4.8
Berekening3 = 8*10
Berekening4 = 3
x = Berekening1 * Berekening2 * Berekening3 + Berekening4
print "Het antwoord van de berekening is:",
print int(x),
print "."
Robo
  • 37
  • 1
  • 1
  • 5

3 Answers3

3

You could use:

print "Het antwoord van de berekening is: {}.".format(x)
cdonts
  • 9,304
  • 4
  • 46
  • 72
3

Don't use print ..,, it adds that space because you told it to with the comma. Use string formatting instead:

print "Het antwoord van de berekening is: {}.".format(x)

Here the {} is a placeholder, a slot into which to put the first argument to the str.format() method. The . follows right after:

>>> x = 42
>>> print "Het antwoord van de berekening is: {}.".format(x)
Het antwoord van de berekening is: 42.

You could use string concatenation too, but that's just more cumbersome:

 print "Het antwoord van de berekening is: " + str(x) + "."
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Wouldn't you like to use Python 3? In Python 3 print is a function that accepts optional keyword arguments that modify its behaviour according to your desires

In [1]: help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

How does this apply to your problem (or, to be honest, to your particular approach to your task that becomes your problem, as per the comment by Padraic)? You have two possibilities

In [2]: print('The result is ', 8775, '.', sep='')
The result is 8775.

In [3]: print('The result is ', end=''); print(8755, end=''); print('.')
The result is 8775.

In [4]:

If you are stuck in Python 2 you can still take advantage of print as a function importing this behaviour from the __future__, using

from __future__ import print_function

in your programs.

If you don't know very much about this import from __future__ stuff, SO is your friend...,

Community
  • 1
  • 1
gboffi
  • 22,939
  • 8
  • 54
  • 85