stormoverparis
stormoverparis

Reputation: 21

How to get the title to print

if we are given this code:

from collections import namedtuple
Book = namedtuple('Book', 'title author year price')
favorite = Book('Adventures of Sherlock Holmes',
                'Arthur Conan Doyle', 1892, 21.50)
another = Book('Memoirs of Sherlock Holmes', 
               'Arthur Conan Doyle', 1894, 23.50)
still_another = Book('Return of Sherlock Holmes',
                     'Arthur Conan Doyle', 1905, 25.00)

how do you get the title of the book in the variable still_another to print?

Upvotes: 0

Views: 43

Answers (1)

Simeon Visser
Simeon Visser

Reputation: 122376

You can do:

print(still_another.title)

Each value in a namedtuple can be accessed via its name and in your case the title can be accessed via .title.

Upvotes: 4

Related Questions