Skip to Content
Python 3 Object-Oriented Programming - Third Edition
book

Python 3 Object-Oriented Programming - Third Edition

by Dusty Phillips
October 2018
Beginner to intermediate
466 pages
12h 2m
English
Packt Publishing
Content preview from Python 3 Object-Oriented Programming - Third Edition

The format method

There are a few cases where you won't be able to use f-strings. First, you can't reuse a single template string with different variables. Second, f-strings were introduced in Python 3.6. If you're stuck on an older version of Python or need to reuse template strings, you can use the older str.format method instead. It uses the same formatting specifiers as f-strings, but can be called multiple times on one string. Here's an example:

>>> template = "abc {number:*^10d}">>> template.format(number=32)'abc ****32****'>>> template.format(number=84)'abc ****84****'

The format method behaves similarly to f-strings, but there are a couple of differences:

  • It is restricted in what it can look up. You can access attributes on objects ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python 3 Object-Oriented Programming - Second Edition

Python 3 Object-Oriented Programming - Second Edition

Dusty Phillips

Publisher Resources

ISBN: 9781789615852Supplemental Content