Flapjack
Flapjack

Reputation: 65

How do I make a new line while returning a string?

I'm doing a class/object exercise in python and everything is working fine apart from this one line. So Im wondering if anyone has a solution to this

Tried a lot of variations with '\n' but none of them worked as expected

def __str__(self):
    return f'Account owner:   {self.owner}\nAccount balance:    ${self.balance}'

Expected it to be returned in 2 lines but it just keeps coming back like this 'Account owner: Jose\nAccount balance: $100'

Ty all in advance!

Upvotes: 0

Views: 62

Answers (2)

Loocid
Loocid

Reputation: 6441

It depends how you're using __str__. What you have done is correct however \n is only interpreted as a newline when you are writing it to something, be it the console or a file.

Example:

>>> a = "a\nb"
>>> a
'a\nb'
>>> print(a)
a
b

Upvotes: 2

André Pacheco
André Pacheco

Reputation: 1885

Try this:

def __str__(self):
    return "Account owner: {}\nAccount balance: ${}".format(self.owner,self.balance)

Upvotes: 0

Related Questions