Python print format

In Python, you have a few options to format your strings. In this article, I will go over

"Hello, my name is {}. I am a {} turned {}."
3, formatted string literals, and template strings.

But first, let's take a look at what is considered to be the "old way" of formatting strings.

What is % string formatting in Python?

One of the older ways to format strings in Python was to use the

"Hello, my name is {}. I am a {} turned {}."
4 operator.

Here is the basic syntax:

"This is a string %s" % "string value goes here"

You can create strings and use

"Hello, my name is {}. I am a {} turned {}."
5 inside that string which acts like a placeholder. Then you can write
"Hello, my name is {}. I am a {} turned {}."
4 followed be the actual string value you want to use.

Here is a basic example using

"Hello, my name is {}. I am a {} turned {}."
4 string formatting.

print("Hi, my name is %s" % "Jessica")
Python print format

This method is often referred to as the "older" way because Python 3 introduced

"Hello, my name is {}. I am a {} turned {}."
3 along with formatted string literals.

What is the "Hello, my name is {}. I am a {} turned {}."3 method in Python?

Here is the basic syntax for the

"Hello, my name is {}. I am a {} turned {}."
3 method:

"template string {}".format(arguments)

Inside the template string, we can use

.format("Jessica", "musician", "programmer")
1 which act as placeholders for the arguments. The arguments are values that will be displayed in the string.

In this example, we want to print

.format("Jessica", "musician", "programmer")
2

In the string, we are going to have a total of three

.format("Jessica", "musician", "programmer")
1 which will act as placeholders for the values of Jessica, musician, and programmer. These are called format fields.

"Hello, my name is {}. I am a {} turned {}."

Inside these parenthesis for the

"Hello, my name is {}. I am a {} turned {}."
3, we will use the values of "Jessica", "musician", and "programmer".

.format("Jessica", "musician", "programmer")

Here is the complete code and printed sentence:

print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
Python print format

Positional arguments

You can access the value of these arguments using an index number inside the

.format("Jessica", "musician", "programmer")
1.

In this example, we have two arguments of

.format("Jessica", "musician", "programmer")
6 and
.format("Jessica", "musician", "programmer")
7 inside the
.format("Jessica", "musician", "programmer")
8.

.format("trumpet", "drums")

We can access those values inside the string by referring to the index numbers.

.format("Jessica", "musician", "programmer")
9 refers to the first argument of
.format("Jessica", "musician", "programmer")
6 and
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
1 refers to the second argument of
.format("Jessica", "musician", "programmer")
7.

"Steve plays {0} and {1}."

Here is the complete code and printed sentence:

print("Steve plays {0} and {1}.".format("trumpet", "drums"))
Python print format

We can modify this example and switch the index numbers in the string. You will notice that the sentence has changed and the placement of the arguments is switched.

print("Steve plays {1} and {0}.".format("trumpet", "drums"))
Python print format

Keyword arguments

These arguments consist of a

print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
3
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
4 pair. We can access the
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
4 of the argument by using the
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
3 inside the
.format("Jessica", "musician", "programmer")
1.

In this example, we have two keys called

print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
8 and
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
9. We are going to use those keys inside the string.

print("Hi, my name is %s" % "Jessica")
0

Inside the

.format("Jessica", "musician", "programmer")
8, we have the
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
3
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
4 pairs.

print("Hi, my name is %s" % "Jessica")
1

Here is the complete code and printed sentence.

print("Hi, my name is %s" % "Jessica")
2
Python print format

How to Mix Keyword and Positional arguments

In the

"Hello, my name is {}. I am a {} turned {}."
3 you can mix keyword and positional arguments.

In this example, we are going to create a short story about going to Disneyland.

We are first going to create a few variables for name, number, adjective and a Disneyland ride.

print("Hi, my name is %s" % "Jessica")
3

We then want to create our string using keyword and positional arguments. I am going to add the

.format("trumpet", "drums")
4 to tell the computer to create a new line after each sentence.

print("Hi, my name is %s" % "Jessica")
4

Inside the parenthesis for the

"Hello, my name is {}. I am a {} turned {}."
3, we will assign our variables to the keys of
.format("trumpet", "drums")
6,
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))
9,
.format("trumpet", "drums")
8 and
.format("trumpet", "drums")
9.
.format("Jessica", "musician", "programmer")
9 will have the value of
"Steve plays {0} and {1}."
1.

print("Hi, my name is %s" % "Jessica")
5

Here is the complete code and printed sentence:

print("Hi, my name is %s" % "Jessica")
6
Python print format

What are formatted string literals?

Formatted string literals (or f-strings) allow you to include expressions inside your strings. Just before the string you place an

"Steve plays {0} and {1}."
2 or
"Steve plays {0} and {1}."
3 which tells the computer you want to use an
"Steve plays {0} and {1}."
4.

Here is the basic syntax:

print("Hi, my name is %s" % "Jessica")
7

Here is a basic example that prints the sentence

"Steve plays {0} and {1}."
5

print("Hi, my name is %s" % "Jessica")
8

It works just the same if I use a capital

"Steve plays {0} and {1}."
3 before the string.

print("Hi, my name is %s" % "Jessica")
9
Python print format

You can also use an

"Steve plays {0} and {1}."
4 to format data from a dictionary.

In this example, we have a dictionary which represents the top rankings for men's college basketball teams and how many games they won out of 32.

"template string {}".format(arguments)
0

We can use a

"Steve plays {0} and {1}."
8 and the
"Steve plays {0} and {1}."
9 method to go through each of the
print("Steve plays {0} and {1}.".format("trumpet", "drums"))
0 pairs of the
print("Steve plays {0} and {1}.".format("trumpet", "drums"))
1 dictionary.

"template string {}".format(arguments)
1

Inside the

"Steve plays {0} and {1}."
8, we can use an
"Steve plays {0} and {1}."
4 to format the printed results.

The use of the

print("Steve plays {0} and {1}.".format("trumpet", "drums"))
4 for  
print("Steve plays {0} and {1}.".format("trumpet", "drums"))
5 and
print("Steve plays {0} and {1}.".format("trumpet", "drums"))
6 tells the computer to create a field that is 10 characters wide. This will create even columns for the data.

The

print("Steve plays {0} and {1}.".format("trumpet", "drums"))
7 inside here
print("Steve plays {0} and {1}.".format("trumpet", "drums"))
6 refers to a decimal integer.

"template string {}".format(arguments)
2

Here is the full code and the printed output:

"template string {}".format(arguments)
3
Python print format

What are template strings?

Template strings are Python strings that use placeholders for the real values.

Here is the basic syntax:

"template string {}".format(arguments)
4

Let's take a look at an example to better understand how it works.

In this example, we want to print

print("Steve plays {0} and {1}.".format("trumpet", "drums"))
9 using template strings.

In order to use template strings, you will first have to import the

print("Steve plays {1} and {0}.".format("trumpet", "drums"))
0 class from the standard library.

"template string {}".format(arguments)
5

You can then use the

print("Steve plays {1} and {0}.".format("trumpet", "drums"))
0 class and provide a string inside the parenthesis. We are going to place a
print("Steve plays {1} and {0}.".format("trumpet", "drums"))
2 in front of
.format("trumpet", "drums")
6 which will later be replaced by the real value.

"template string {}".format(arguments)
6

We then add

print("Steve plays {1} and {0}.".format("trumpet", "drums"))
4 to the template and assign the value of
print("Steve plays {1} and {0}.".format("trumpet", "drums"))
5 to
.format("trumpet", "drums")
6.

"template string {}".format(arguments)
7

Here is the full code and the printed output:

"template string {}".format(arguments)
8
Python print format

Conclusion

There are many ways to format your strings in Python.

The older way of formatting your strings would be to use the

"Hello, my name is {}. I am a {} turned {}."
4 operator.

"template string {}".format(arguments)
9

"Hello, my name is {}. I am a {} turned {}."
5 acts as a placeholder for the real value. You place the real value after the
"Hello, my name is {}. I am a {} turned {}."
4 operator.

This method is often referred to as the "older" way because Python 3 introduced

"Hello, my name is {}. I am a {} turned {}."
3 and formatted string literals (f-strings).

In the

"Hello, my name is {}. I am a {} turned {}."
3 method, you use
.format("Jessica", "musician", "programmer")
1 for placeholders and place the real values inside the parenthesis. This method can take in positional and keyword arguments.

"template string {}".format(arguments)

Formatted string literals (or f-strings) allow you to include expressions inside your strings. Just before the string you place an

"Steve plays {0} and {1}."
2 or
"Steve plays {0} and {1}."
3 which tells the computer you want to use an
"Steve plays {0} and {1}."
4.

print("Hi, my name is %s" % "Jessica")
7

You can also use Template strings by importing the

print("Steve plays {1} and {0}.".format("trumpet", "drums"))
0 class from the standard library. Template strings are Python strings that use placeholders for the real values.

"template string {}".format(arguments)
4

I hope you found this article helpful and best of luck on your Python journey.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Python print format
Jessica Wilkins

I am a musician and a programmer.


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

How do you print format in Python?

There are several ways to format output. To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.

What is %d %s in Python?

The%s acts a placeholder for a string while %d acts as a placeholder for a number. The associated values of them are then passed in through a tuple using the % operator. name = 'marcog' number = 42 print '%s %d' % (name, number) The above information will print marcog 42.

What is %d and %i in Python?

Here's what python.org has to say about %i: Signed integer decimal. And %d: Signed integer decimal. %d stands for decimal and %i for integer. but both are same, you can use both.

Is printf () and format () are same?

The key difference between them is that printf() prints the formatted String into console much like System. out. println() but the format() method returns a formatted string, which you can store or use the way you want.