-3
class car():
    def __init__(self,name,model,colour,pricerange,state):
        self.name = name
        self.model = model
        self.colour = colour
        self.pricerange = pricerange
        self.state = state

    def fthree(self):
        print("Name: "+self.name)
        print("Model: " + str(self.model))
        print("Colour: " + self.colour)

Output:

Name: tiago

Model: 2016

Colour: white

I am not using '\n'. But the control is being shifted to the next line in the output. Does this always happen when working with a class in Python?

Austin
  • 25,759
  • 4
  • 25
  • 48
  • Try it without a class and see that it's unrelated. – Jonathon Reinhart Jan 27 '19 at 07:42
  • 1
    Please indicate, using a tag, whether you are using Python 2 or 3. I'm assuming Python 3, due to the `print` function call. – Jonathon Reinhart Jan 27 '19 at 07:44
  • 2
    Cannot reproduce with `c = Car('a','b','c',10,'ca'); c.fthree()` - I don't get blank lines. – wwii Jan 27 '19 at 07:47
  • The code you show us doesn't, by itself, have this problem. Perhaps you are calling it with an incorrect value with a trailing newline in the first place. You should [edit] your question into a [mcve]. – tripleee Jan 27 '19 at 07:58

1 Answers1

0

The print function (in Python 3) defaults to printing a newline character:

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

Print objects to the text stream file, separated by sep and followed by end.

If you don't want this behavior, pass an empty string for end:

print("hello ", end="")
print("world")
Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328