Format strings with str.format Python 3


show more Formatting strings with str.format provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Formatting strings with str.format

The string format method is very powerful and useful tool. You'll see it used a lot and you will use it a lot. It looks like this. Let's start by assigning a couple of variables a and b = 5 and 42. And then we'll print them out. If I were to just say print a, b like that then we get the numbers. But that's not terribly useful or descriptive. And you might want to look at it more like this. You might want to say this is and put in a placeholder and that is and put in another placeholder.

And then use the format method and say a and b. And now you get a string, 'this is 5 and that is 42.' So this is what the format method does. It allows you to put placeholders in a string and then replace those placeholders with values and return a new string. It's important to note that the string that's returned is a new string. Strings are immutable in Python. So this string is an entirely different string than that string. So if I had a string that just had this in it, do a ...

Formatting strings with str.format
Video duration: 5m 31s 6h 36m Beginner

Viewers:

Formatting strings with str.format provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...