__format__() method implements the built-in
format() function as well as the
string.format() method. So, when you call
format(x, spec) or
string.format(spec), Python attempts to call
x.__format__(spec). The return value is a string.
We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.
format(value, spec) function transforms input of one format into output of another format defined by you.
Specifically, it applies the format specifier
spec to the argument
value and returns a formatted representation of
format(42, 'f') returns the string representation
Example Custom __format__()
In the following example, you create a custom class
Data and overwrite the
__format__() magic method so that it returns a dummy string
class Data: def __format__(self, spec): return 'hello ' + spec x = Data() print(format(x, 'world'))
This way, you can create your own little formatting language for a custom object.
class Data: def __format__(self, spec): if spec == '42': return 'finxter' return 'hello ' + spec x = Data() print(format(x, 'world')) # hello world print(format(x, '42')) # finxter
In the first
print() statement, you use the format specifier
'world' which has no special meaning in your custom implementation of the
__format__() method. Thus, the output is
'hello world'—the result of the string concatenation
'hello' and the format specifier
In the second
print() statement, you use the special (according to your own definition) format specifier
__format__() method now returns a special string: ‘finxter’!
format()Function: No-BS Guide by Example
- Python String
- String Formatting: Keep It Simple, Stupid!
Related Video: Python String Methods
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.