Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

String Formatting With the .format() Method

The .format() method is a newer formatting method that should be preferrerd over %-style formatting. In this lesson, you’ll learn when you should use it, how to produce powerful format strings without complicating simpler use cases, and how to format an int variable as a hexadecimal string:

Python
>>> 'Hey, {name}, there is a 0x{errno:x} error'.format(name=name, errno=errno)
'Hey Bob, there is a 0xbadc0ffee error!'

00:00 Moving on to method two, string formatting with .format(), or the format method. It was found that string formatting with the interpolation or percent sign (%) operator had a few quirks that would sometimes lead to funky output or errors. To rectify that, somewhere around Python 2.6 the .format() method was released, and that added some functionality and removed some of the quirks that were found with the interpolation operator.

00:33 So, when to use the .format() method? Again, with legacy code and if you’re coding in Python anywhere between 2.6 and 3.6, the .format() method will be your prevalent way of string formatting. Once again, the best way to learn is to hop into the scenario, so let’s hop over to the console and get started. Back in the console, just to review very quickly, we have two variables initialized, errno and name, containing an integer and a string. With the .format() method, you’re simply calling a method on a string.

01:11 So, try this with me. Just like before, we have to indicate where we want to substitute our variable, and with the .format() method that’s done with curly brackets somewhere in our string.

01:27 And then we call the .format() method and pass it the variable that we want to substitute. 'Hello, Bob'! And just like our previous example, we can refer to our variables by name and use them in any order we want with key pair values. Give it a try in your own console.

01:51 Take a look here. We can still select the output formatting. We need our errno integer to be hex. And so, with the .format() method, output formatting selection is done with the colon (:) and the character.

02:11 And this time when we call .format(), we need to give it the keywords and the variable we want to substitute.

02:23 I’ll scroll so you can see the whole thing.

02:27 'Hey {name}, there is a 0x{errno:x} error!' and then we called the .format() method and passed it the variables. So once again, this newer style of string formatting is preferred over the old interpolation method because that old method had a few quirks and the new method is a more powerful text formatter.

Avatar image for nmsandy

nmsandy on Aug. 28, 2019

with all tutorial in the python 3

Become a Member to join the conversation.