Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

String Formatting With the % Operator

The interpolation operator % is used to do simple positional formatting:

Python
>>> name = 'Bob'
>>> 'Hello, %s' % name
"Hello, Bob"

In this lesson, you’ll learn how to use % to format strings as well as the different format specifiers you can use to generate nicely formatted output.

00:00 This video will cover the first major method of formatting strings, and that’s using the modulo or interpolation or string formatting operator (%).

00:12 Now, this is an old-school method of string formatting. It’s not in common practice anymore, but you might bump into it from time to time. So, when do we use the interpolation operator?

00:24 You’ll see it used in legacy code in Python implementations using Python 2.6 or earlier. Let’s jump back into our scenario in the console and try out method one, old-school string formatting. Okay. Stepping back into our example, remember, we have two variables initialized—errno, containing an integer, and name, containing a string 'Bob'.

00:53 And when we use this method one, old-school string formatting, we’re going to use the interpolation percent sign operator. All we do with the interpolation operator is bookmark where we want to substitute the variable. So, give this a try with me.

01:12 We’re going to put our name variable right here and we need it to be output as a string. I’ll talk about that more in a second. After the string is closed, we need to put the interpolation operator and the variable that we want to give our string. Give that a go, 'Hello, Bob'.

01:28 So, where I put that %s to signify I wanted a string, there are other format specifiers available, and these let you control the output format of your string.

01:38 You can do things like hexadecimal notation, whitespace padding, or floating-point decimals, just to name a few. So, we want hexadecimal for our integer, so we’re going to do that by specifying hexadecimal with the letter x.

01:51 So, let’s do that in a string, and we’ll see what happens when we give it our integer in our errno variable. 'badc0ffee'. So, now we want to combine these two into a string, and when we do that, we have to make a slight syntax change at the end when we’re giving our string the variables that we want to substitute. So follow along with me here and we’ll take a look at that.

02:22 'There is a 0x', which in programming indicates that a hexadecimal is to follow. So now the string is closed and we’re going to give it the variables that we want to substitute. When we do that with multiple variables, we have to give the string a tuple with the variables you want to substitute in the order that we want to substitute them.

02:47 Let’s print that, 'Hey Bob, there is a 0xbadc0ffee error!' So, you can imagine if you’re writing a lot of code or you have a long string with a lot of variables in it, that keeping things straight could get confusing.

03:01 So another option you have with interpolation is to refer to variables by name within the format string. This will make more sense with an example, so try this with me.

03:29 Using this method, when it’s time to tell it what variables you want to substitute, you have to give it a dictionary of key-value pairs.

03:45 So, where we see %(name)s, we want to substitute the name variable. And where we see %(errno)x,

03:55 let’s substitute the errno variable. Close that off, and let me scroll so you can see the whole string here. 'Hey %(name)s, there is a 0x%(errno)x error!' And then we give it our key pair values that we want to substitute.

04:14 So, let’s print that out. 'Hey Bob, there is a 0xbadc0ffee error!' So using the key pair values method, this means that you don’t have to worry about the order that you’re passing the values, like you did with the tuple.

04:27 So, that was method one, old-school style string formatting.

Patricio Urrutia on June 10, 2020

Hi, thanks for the course!

What do you mean with Legacy Code?

Daniel Faria on July 3, 2020

badc0ffee – that made me laugh hard

useeme2ndtime on April 28, 2021

Patricio Urrutia legacy code Paul means Python 2.6 or earlier.

Become a Member to join the conversation.