0

I am writing a small function; the result must be displayed in multiple lines; how to do this

def display(self):
    return f'Name: {self.name},\n Roll no:{self.rollno}'
Kabilan Mohanraj
  • 1,856
  • 1
  • 7
  • 17
acoustic python
  • 259
  • 1
  • 2
  • 12
  • 1
    Does this answer your question? [Pythonic way to create a long multi-line string](https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string) – jitter Jun 24 '21 at 03:55
  • Well, you seem to have already written a function that *returns* a multi-line string. So what exactly are you asking? What do you mean by *displayed*? Your function seems to be a class method (judging by the `self` parameter), so if you have an instance called `obj` and you simply call `print(obj.display())` for example, the string will be correctly displayed in two lines (if that's what you are asking). – at54321 Jun 24 '21 at 05:04

1 Answers1

0

You can use """ or ''' to start and end a multiline string in Python. Example:

def try_print_this():
    return """This is
a multiline
string in Python"""

print(try_print_this())

In your case that should be:

def display(self):
    return f'''Name: {self.name},
Roll no:{self.rollno}'''
bruno-uy
  • 1,647
  • 12
  • 20