livepython.net

186. Python Program to Print a Book Object Using __str__

Easy__str__

Overview

Teach a class how to print itself with __str__.

Problem Statement

Define a class Book with three attributes: title, author and pages (a whole number). Give it a __str__ method that returns a description like this one:

Python Basics by Sunny, 120 pages

Read the title, the author and the number of pages, create a Book, and print it with print(book). Don't print the attributes one by one: let __str__ do the work.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Input

Three lines: the title, the author, then the pages. Example:

Python Basics
Sunny
120

Output

The output for that example:

Python Basics by Sunny, 120 pages

The tests try 3 different inputs.

Learn this firstChapter 20 · Python __str__ Method: Print Objects Nicely
solution.py
# write your code here

EXPECTED OUTPUT

1Python Basics by Sunny, 120 pages

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.