Skip to main content

Python String format() Tutorial

Learn about string formatting in Python.
Oct 2020  · 5 min read

String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text.

custom_string = "String formatting"
print(f"{custom_string} is a powerful technique")
String formatting is a powerful technique

As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.

Methods for Formatting

  • Positional formatting
  • Formatted string literals
  • Template method

String.Format Method

We put placeholders defined by a pair of curly braces in a text. We call the string dot format method. Then, we pass the desired value into the method. The method replaces the placeholders using the values in the order of appearance replace by value:

'text{}'.format(value)

Positional Formatting

We define a string and insert two placeholders. We pass two strings to the method, which will be passed to get the following output:

print("Machine learning provides {} the ability to learn {}".format("systems", "automatically"))
Machine learning provides systems the ability to learn automatically

We can use variables for both the string and the values passed to the method. In the below example code, we define a string with placeholders and two other variables. We apply the format method to the string using the two defined variables. The method reads the string and replaces the placeholders with the given values.

my_string = "{} rely on {} datasets"
method = "Supervised algorithms"
condition = "labeled"
print(my_string.format(method, condition))
Supervised algorithms rely on labeled datasets

Reordering Values

In the below example, you add index numbers into the placeholders to reorder values. This affects the order in which the method replaces the placeholders.

The method replaces them with the values in the given order.

print("{} has a friend called {} and a sister called {}". format("Betty", "Linda", "Daisy"))
Betty has a friend called Linda and a sister called Daisy

If we add the index numbers, the replacement order changes accordingly.

print("{2} has a friend called {0} and a sister called {1}". format("Betty", "Linda", "Daisy"))
Daisy has a friend called Betty and a sister called Linda

Name Placeholders

We can also introduce keyword arguments that are called by their keyword name.

In the example code below, we inserted keywords in the placeholders. Then, we call these keywords in the format method. We then assign which variable will be passed for each of them, resulting in the following output.

tool="Unsupervised algorithms"
goal="patterns"
print("{title} try to find {aim} in the dataset".format(title=tool, aim=goal))
Unsupervised algorithms try to find patterns in the dataset

Let's examine this code below. We have defined a dictionary with keys: tool and goal.

my_methods = {"tool": "Unsupervised algorithms", "goal": "patterns"}

We want to insert their values in a string. Inside the placeholders, we can specify the value associated with the key tool of the variable data using bracket notation. Data is the dictionary specified in the method, and tool is the key present in that dictionary.

print('{data[tool]} try to find {data[goal]} in the dataset'.format(data=my_methods))

So, we get the desired output shown below. Be careful! You need to specify the index without using quotes.

Unsupervised algorithms try to find patterns in the dataset

Format Specifier

We can also specify the format specifies inside curly braces. This defines how individual values are presented. Here, we’ll use the syntax index colon specifier. One of the most common format specifiers is float represented by f. In the code, we specify that the value passed with the index 0 will be a float.

print("Only {0:f}% of the {1} produced worldwide is {2}!". format(0.5155675, "data", "analyzed"))
Only 0.515567% of the data produced worldwide is analyzed!

We could also add .2f indicating that we want the float to have two decimals, as seen in the resulting output.

print("Only {0:.2f}% of the {1} produced worldwide is {2}!".format(0.5155675, "data", "analyzed"))
Only 0.52% of the data produced worldwide is analyzed!

Formatting datetime

Python has a module called datetime that allows us to, for example, to get the time and date for today.

from datetime import datetime
print(datetime.now())
2020-08-08 06:28:42.715243

But since the format returned is very particular, you could use the format specifier such as %y-%m-%d-%h-%m to adjust the format to something more familiar to us!

print("Today's date is {:%Y-%m-%d %H:%M}".format(datetime.now()))
Today's date is 2020-08-08 06:29

Interactive Example

In the following example, you will assign the substrings going from the 4th to the 19th character, and from the 22nd to the 44th character of wikipedia_article to the variables first_pos and second_pos, respectively. Adjust the strings so they are lowercase. Finally, print the variables first_pos and second_pos.

# Assign the substrings to the variables
first_pos = wikipedia_article[3:19].lower()
second_pos = wikipedia_article[21:44].lower()

When we run the above code, it produces the following result:

computer science artificial intelligence

Try it for yourself.

To learn more about positional formatting, please see this video from our course, Regular Expressions in Python.

This content is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.

Check out our Python String Tutorial.

Introduction to Python

Beginner
4 hours
4,590,937
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course

Intermediate Python

Beginner
4 hours
881,943
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Python Data Science Toolbox (Part 1)

Beginner
3 hours
344,007
Learn the art of writing your own functions in Python, as well as key concepts like scoping and error handling.
See all coursesRight Arrow
Related

The 23 Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.
Abid Ali Awan's photo

Abid Ali Awan

22 min

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
DataCamp Team's photo

DataCamp Team

Plotly Express Cheat Sheet

Plotly is one of the most widely used data visualization packages in Python. Learn more about it in this cheat sheet.
DataCamp Team's photo

DataCamp Team

0 min

Getting started with Python cheat sheet

Python is the most popular programming language in data science. Use this cheat sheet to jumpstart your Python learning journey.
DataCamp Team's photo

DataCamp Team

8 min

Python pandas tutorial: The ultimate guide for beginners

Are you ready to begin your pandas journey? Here’s a step-by-step guide on how to get started. [Updated November 2022]
Vidhi Chugh's photo

Vidhi Chugh

15 min

Python Iterators and Generators Tutorial

Explore the difference between Python Iterators and Generators and learn which are the best to use in various situations.
Kurtis Pykes 's photo

Kurtis Pykes

10 min

See MoreSee More