Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

String Formatting

In this lesson, you’ll get a quick introduction to formatting strings. Python supports three styles of string formatting:

'Hello %s' % name    # C-style

'My name is {1}, {0} {1}'.format('James', 'Bond')  # Added in Python 3.0

f'I am {age} years old'  # f-string added in Python 3.6

Here are some examples of C-style and newer f-string formatting:

>>>
>>> first = 'James'
>>> last = 'Bond'
>>> age = 90
>>> message = 'No, Mr. %s, I expect you to die' % last
>>> print(message)
No, Mr. Bond, I expect you to die
>>> print('The name is %s, %s %s' % (last, first, last))
The names is Bond, James Bond
>>> print('Sean Connery is now %d years old' % age)
Sean Connery is now 90 years old
>>> print('pi: %f \nshort pi %0.2f' % (math.pi, math.pi))
pi: 3.141593
short pi 3.14
>>> message = f'No, Mr. {last}, I expect you to die'
>>> print(message)
No, Mr. Bond, I expect you to die
>>> print(f'The name is {last}, {first} {last}')
The name is Bond, James Bond
>>> print(f"Sean's age times pi is {age*math.pi}")
Sean's age times pi is 282.7433388230814
>>> print(f"Sean's age times pi is {age*math.pi:.2f}")
Sean's age times pi is 282.74

To learn more, check out the following resources:

00:00 In the previous lesson, I introduced you to print() and string literals. In this lesson, I’ll spend a little bit of time on how to format strings so that you can create more complicated messages inside of your print() function.

00:12 First off, you can concatenate two strings to create a third one. "Hello " + "World!" would result in a third string called "Hello World!".

00:22 You have to be a little careful in Python. This doesn’t actually edit the string—it creates a third, brand new string. This can make string manipulation a little memory intensive and slow in performance.

00:33 Over the years, Python has added several ways of manipulating strings. The first is similar to C, supporting the %s notation. The second has placeholders using curly brackets. And the third, which is fairly recent, is an f-string.

00:49 Generally, it’s recommended to use the f-string as much as possible but there’s still a couple cases where C-style might be important. I’ll show you how to use these two in the terminal right now.

01:00 I’m going to start off by creating a few variables here: first name, last name, age. And the first formatting example is %s. The '%s' in this string is a placeholder for where the content is going to be injected.

01:14 The percent (%) at the end of the string says what to inject. In this case, the value of last name gets put into the string.

01:23 You can put multiple percents inside of a format. In order to do that, you pass a tuple to the string instead of a single value. Notice if you want to use the last name twice, you have to have two %s’s—one for each instance of the last name and the variable twice inside of the tuples. %s says that you’re going to be putting a string in. %d says you’re going to put in an integer.

01:51 And that leaves us with the very depressing statement that the only true Bond is now 90 years old. %f is for floats.

02:01 In addition to the indicator, you can also put formatting information. The 0.2 here tells Python to put as many digits to the left of the decimal as you like, but only 2 significant digits to the right.

02:15 This formatting will work in more than just floats. It will also work in %d’s and %s’s. Python 3 introduced the concept of an f-string.

02:25 The f in front of the string indicates that this is a formatting string. Now, curly brackets indicate where to put the injection.

02:34 This is the same as the first example. Last name gets injected, and we end up with No, Mr. Bond, I expect you to die.

02:44 I find this mechanism far easier to read, particularly if you’ve got multiple variables. Our famous movie quote here has last twice, and looking at the string, you can see exactly what it is without having to look at the end of a tuple to figure out what is being injected. Format strings support calculation.

03:01 I don’t know why you would want to multiply an age times pi, but you can.

03:08 They also support formatting. Inside of the curly brackets, you put a colon (:) indicating your format mechanism. The .2f is similar to the 0.2f above, giving us Sean Connery’s age times pi, to 2 significant digits.

03:25 This was only meant as a quick introduction. There are full videos and text articles on Real Python about f-strings if you want to see all of their great power.

03:35 The next section is on finer-grained control of print() by using the sep, end and flush arguments.

dngrant on Oct. 9, 2020

I absolutely LOVE the Goldfinger reference from Mr. Trudeau

Become a Member to join the conversation.