nltk / nltk_book

NLTK Book
http://www.nltk.org/book
403 stars 143 forks source link

Chapter 3: replace old %-style formatting #227

Closed pjhinton closed 4 years ago

pjhinton commented 4 years ago

There are a couple of instances in Chapter 3 where string formatting is done with the percent infix operator rather than str.format().

The first is in the subsection Lining Things Up:

'{:{width}}' % ("Monty Python", width=15)

which can be expressed as:

'{:{width}}'.format('Monty Python', width=15)

and in the subsection Text Wrapping

pieces = [format % (word, len(word)) for word in saying]

which can be expressed as:

template = '{} {}'
pieces = [template.format(word, len(word)) for word in saying]