Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is .format in Python

Understanding .format in Python

When you're starting out in the world of programming, getting acquainted with the basics can make a huge difference. One such fundamental concept in Python is string formatting, and the .format() method plays a central role in this. It's a powerful tool that allows you to create neat and dynamically updated strings. Let's dive into what .format() is and how you can use it to make your code more readable and efficient.

The Basics of String Formatting

Imagine you're writing a letter, and there are certain details like the recipient's name, the date, and some personal details that change every time. Instead of writing a new letter from scratch for each person, you use a template with placeholders that you fill in with the appropriate information. This is what .format() does with strings in Python.

A string is a sequence of characters (like words and sentences) that Python recognizes as text. When you're programming, you often need to create strings that include variable data—numbers, names, or other values that change depending on the context of your program.

The .format() Method Explained

The .format() method is a string method, which means it's a function that specifically works with strings. It allows you to insert, or "format," objects into a string. The placeholders within the string are defined using curly braces {}. You can think of these placeholders as empty boxes that .format() will fill with the data you provide.

Here's a simple example:

welcome_message = "Hello, {}. Welcome to {}!"
print(welcome_message.format("Alice", "Wonderland"))

In this code, "Alice" replaces the first {} and "Wonderland" replaces the second {}. The output will be:

Hello, Alice. Welcome to Wonderland!

Positional and Keyword Arguments in .format()

You can use both positional and keyword arguments to pass values into .format(). Positional arguments are values that are inserted in order according to their position. For example:

greeting = "Good {0}, {1}! Your first {0} task is to {2}."
print(greeting.format("morning", "Bob", "check emails"))

This will output:

Good morning, Bob! Your first morning task is to check emails.

Notice how {0} is used twice? It's replaced by the first argument of .format(), which is "morning".

Keyword arguments, on the other hand, are arguments with names, which makes your code even clearer:

greeting = "Good {time_of_day}, {person}! Your first {time_of_day} task is to {activity}."
print(greeting.format(time_of_day="morning", person="Bob", activity="check emails"))

This will give you the same output as before, but the placeholders now have meaningful names, making the template easier to read and understand.

Formatting Numbers and Variables

.format() is also very handy when you need to include numbers in your strings, especially if you want to control the way they look. Say you want to display a number to two decimal places:

price = 9.99
announcement = "The price of this book is {:.2f} dollars."


The price of this book is 9.99 dollars.

The :.2f inside the curly braces tells Python to format the number as a floating-point number with two decimal places.

Using .format() with Dictionaries

You can also use .format() with dictionaries, which are collections of key-value pairs in Python. Here's how:

person = {'name': 'Eric', 'age': 30}
intro = "My name is {name} and I am {age} years old."

The double asterisk ** is used to unpack the dictionary, so the keys are used as the keyword arguments for .format().

Advanced Formatting Options

.format() can do much more than just replace placeholders with values. It can also align text, pad numbers with zeros, and format dates. For example, you can align text to the left, right, or center within a certain space:

text = "|{:<10}|{:^10}|{:>10}|"
print(text.format('left', 'center', 'right'))

This will output text aligned in different ways within a field of 10 characters:

|left      |  center  |     right|

Intuitions and Analogies

If this still feels a bit abstract, think of .format() like a more advanced version of filling in blanks in a sentence. The curly braces {} are like blank spaces in a Mad Libs game where you're asked to insert an adjective, noun, or verb. The .format() method then takes your list of words (arguments) and places them into these blanks to complete the story.


Mastering the .format() method in Python can significantly improve the way you handle and display text in your programs. It's a bit like learning to cook; once you know the basic recipe, you can start experimenting with different ingredients. As you practice, you'll find more creative and efficient ways to convey information through strings.

Remember, coding is as much about communication as it is about solving problems. By using .format() effectively, you're not just writing code that works, you're writing code that speaks clearly. Keep experimenting with different formatting options and soon you'll be able to tailor your strings in Python as comfortably as crafting a personalized letter. Happy coding!