0
class Direction:
    def __init__(self,position=0):
        self.position=position
        self.dir=1
    def display(self):
        print("Positon:",self.position,",Direction: Up")

Here is what's printing now:

 >>> a=Direction(3)

 >>> a.display()

 Positon: 3 ,Direction: Up

I want to remove the space after 3.

wwii
  • 23,232
  • 7
  • 37
  • 77
Mark
  • 11
  • 3
  • You're not showing the code that actually does the printing. That said, `print` adds spaces between elements being printed. You can pass `sep=''` to make the separator the empty string instead. – ShadowRanger Nov 15 '16 at 01:36
  • You can `print(..., sep='')` to remove the default space. But probably better would be to use `print("Positon: {}, Direction {}".format(self.position, ["Down", "Up"][self.dir]))` [I'm assuming `"Up"` is actually calculated from `self.dir`. – AChampion Nov 15 '16 at 01:38
  • 4
    FYI, this is a duplicate of [How to print in Python without newline or space?](http://stackoverflow.com/q/493386/364696). I'd close, but I clicked the wrong option on close vote and can't fix. – ShadowRanger Nov 15 '16 at 01:39
  • You want string formatting. The comma is adding the spaces. https://pyformat.info – OneCricketeer Nov 15 '16 at 01:42

2 Answers2

2

Use format. Your display function would look like this:

def display(self):
    print("Positon: {},Direction: Up".format(self.position))

>>> a.display()

Positon: 3,Direction: Up
Diego Mora Cespedes
  • 3,605
  • 5
  • 26
  • 33
0

Like this:

>>> print("Position: ", self.position, ", Direction: Up", sep='')
Position: 3, Direction: Up

Or:

>>> print("Position: %s, Direction: Up" % self.position)
Position: 3, Direction: Up
Steven Summers
  • 5,079
  • 2
  • 20
  • 31
Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39