|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/videos/string-format-method/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/videos/string-format-method/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Fvideos%2Fstring-format-method%2F |
| https://realpython.com/courses/formatting-python-strings/#team |
| Formatting Python Strings | https://realpython.com/courses/formatting-python-strings/ |
| Liam Pulsifer | https://realpython.com/courses/formatting-python-strings/#team |
| Recommended Tutorial | https://realpython.com/python-formatted-output/ |
| Course Slides (.pdf) | https://realpython.com/courses/formatting-python-strings/downloads/string-formatting-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/formatting-python-strings/downloads/string-formatting-sample-code/ |
| Ask a Question | https://realpython.com/videos/string-format-method/#discussion |
| https://realpython.com/feedback/survey/course/formatting-python-strings/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/formatting-python-strings/disliked/?from=lesson-title |
| Transcript | https://realpython.com/videos/string-format-method/#transcript |
| Discussion | https://realpython.com/videos/string-format-method/#discussion |
| 00:00 | https://realpython.com/videos/string-format-method/#t=0.54 |
| In this lesson, | https://realpython.com/videos/string-format-method/#t=0.54 |
| I’ll show you the basics of how the string .format() method works. | https://realpython.com/videos/string-format-method/#t=1.38 |
| 00:07 | https://realpython.com/videos/string-format-method/#t=7.19 |
| When I talk about string formatting, | https://realpython.com/videos/string-format-method/#t=7.19 |
| what I really mean is you need to have some method for interpolating Python | https://realpython.com/videos/string-format-method/#t=9.83 |
| values into pre-built strings. And by interpolation, | https://realpython.com/videos/string-format-method/#t=14.54 |
| I simply mean inserting those Python values into some pre-built string at a | https://realpython.com/videos/string-format-method/#t=18.5 |
| particular location and with a particular format. | https://realpython.com/videos/string-format-method/#t=24.17 |
| 00:27 | https://realpython.com/videos/string-format-method/#t=27.74 |
| This is useful for creating dynamic text—that is, text | https://realpython.com/videos/string-format-method/#t=27.74 |
| that responds to the events of the runtime of the Python program. | https://realpython.com/videos/string-format-method/#t=31.6 |
| 00:36 | https://realpython.com/videos/string-format-method/#t=36.17 |
| The syntax is pretty simple. You have a template string, | https://realpython.com/videos/string-format-method/#t=36.17 |
| and then you call the .format() method of that string with some number of | https://realpython.com/videos/string-format-method/#t=39.2 |
| positional arguments or keyword arguments, or both. | https://realpython.com/videos/string-format-method/#t=43.1 |
| 00:46 | https://realpython.com/videos/string-format-method/#t=46.67 |
| The template string can contain replacement fields, | https://realpython.com/videos/string-format-method/#t=46.67 |
| which are really where the power of this .format() method comes into play. | https://realpython.com/videos/string-format-method/#t=49.58 |
| These replacement fields are generally pairs of curly braces, | https://realpython.com/videos/string-format-method/#t=53.42 |
| but then you can also have dictionary accesses, object field accesses, | https://realpython.com/videos/string-format-method/#t=57.08 |
| and so on inside those curly braces. And as I’ll show you later, | https://realpython.com/videos/string-format-method/#t=61.7 |
| you can also have some really complex formatting that you can provide those | https://realpython.com/videos/string-format-method/#t=65.78 |
| things inside the braces. | https://realpython.com/videos/string-format-method/#t=69.83 |
| 01:11 | https://realpython.com/videos/string-format-method/#t=71.81 |
| But those braces’ replacement fields are substituted for by the arguments to the | https://realpython.com/videos/string-format-method/#t=71.81 |
| .format() method. | https://realpython.com/videos/string-format-method/#t=75.92 |
| So essentially, you put in curly brace pairs into your string where you want to | https://realpython.com/videos/string-format-method/#t=76.97 |
| substitute the arguments that you pass to .format(), | https://realpython.com/videos/string-format-method/#t=81.47 |
| and those arguments are real Python variables. | https://realpython.com/videos/string-format-method/#t=84.23 |
| 01:27 | https://realpython.com/videos/string-format-method/#t=87.92 |
| Let’s see this work in the Python REPL. I’m here in the REPL, | https://realpython.com/videos/string-format-method/#t=87.92 |
| and if I type in a string—say, "Hello", or something like that— | https://realpython.com/videos/string-format-method/#t=92.63 |
| and then I say .format(), | https://realpython.com/videos/string-format-method/#t=97.07 |
| I’ll just get that this method is a at | https://realpython.com/videos/string-format-method/#t=99.29 |
| this memory location. | https://realpython.com/videos/string-format-method/#t=104.18 |
| 01:45 | https://realpython.com/videos/string-format-method/#t=105.5 |
| So that just goes to show you that .format() is a built-in method of the str (string) | https://realpython.com/videos/string-format-method/#t=105.5 |
| object, so you don’t need to import anything to actually use it. | https://realpython.com/videos/string-format-method/#t=109.19 |
| 01:53 | https://realpython.com/videos/string-format-method/#t=113.21 |
| Now let’s check out some basic usage. So for example, | https://realpython.com/videos/string-format-method/#t=113.21 |
| I could say something like, "Hello, " | https://realpython.com/videos/string-format-method/#t=116.24 |
| and then a replacement string here—a replacement field, I should say. | https://realpython.com/videos/string-format-method/#t=118.22 |
| 02:03 | https://realpython.com/videos/string-format-method/#t=123.89 |
| I’ll say .format() and I’ll just pass in my own name, "Liam", | https://realpython.com/videos/string-format-method/#t=123.89 |
| and it will say, 'Hello, Liam'. Now, of course, if you wanted to | https://realpython.com/videos/string-format-method/#t=127.73 |
| you could make this into a nice function that would just take in a name and | https://realpython.com/videos/string-format-method/#t=130.669 |
| would return this string formatted with that name, | https://realpython.com/videos/string-format-method/#t=134.06 |
| and you could greet anybody you liked. | https://realpython.com/videos/string-format-method/#t=136.19 |
| 02:17 | https://realpython.com/videos/string-format-method/#t=137.81 |
| I’ll leave that as an exercise for you. | https://realpython.com/videos/string-format-method/#t=137.81 |
| Now I can put in as many replacement fields as I like, | https://realpython.com/videos/string-format-method/#t=141.08 |
| and if I just pass in arguments to .format()—so, "Hi", | https://realpython.com/videos/string-format-method/#t=144.38 |
| "Hello", "HOw are you"— | https://realpython.com/videos/string-format-method/#t=148.79 |
| and I’ll just leave that weird capitalization, it’s not going to hurt anything— | https://realpython.com/videos/string-format-method/#t=152.51 |
| then all of those will get substituted in the order that the replacement fields | https://realpython.com/videos/string-format-method/#t=155.54 |
| appear, matched to the order of the arguments themselves. Now, | https://realpython.com/videos/string-format-method/#t=159.86 |
| if I don’t want that behavior, | https://realpython.com/videos/string-format-method/#t=164.48 |
| I can go back here and I can put in the indices of the order that I want those | https://realpython.com/videos/string-format-method/#t=166.04 |
| things in. So as you can see now, | https://realpython.com/videos/string-format-method/#t=171.41 |
| the last element comes first, followed by the first element, followed by the | https://realpython.com/videos/string-format-method/#t=173.57 |
| middle element. And that’s just because I specified the indices here. Now, | https://realpython.com/videos/string-format-method/#t=177.41 |
| you can also put in keywords if you want. So for example, | https://realpython.com/videos/string-format-method/#t=181.63 |
| I could say something like "{a} {b} {c}", | https://realpython.com/videos/string-format-method/#t=184.69 |
| and then as long as my .format() arguments contain keyword arguments of a, | https://realpython.com/videos/string-format-method/#t=189.93 |
| 03:15 | https://realpython.com/videos/string-format-method/#t=195.96 |
| b, and c— | https://realpython.com/videos/string-format-method/#t=195.96 |
| 03:19 | https://realpython.com/videos/string-format-method/#t=199.68 |
| as long as your actual arguments contain those three different keywords | https://realpython.com/videos/string-format-method/#t=199.68 |
| that you’re trying to pass in, then they will be substituted correctly. | https://realpython.com/videos/string-format-method/#t=203.85 |
| That’s nice if you have variables whose names may change or something like | https://realpython.com/videos/string-format-method/#t=207.39 |
| that, but you just know that as long as you have the right keywords, | https://realpython.com/videos/string-format-method/#t=211.74 |
| then everything will work out just fine. | https://realpython.com/videos/string-format-method/#t=215.04 |
| 03:36 | https://realpython.com/videos/string-format-method/#t=216.66 |
| And it can also be nice because then you can have nice semantic meaning. | https://realpython.com/videos/string-format-method/#t=216.66 |
| So for example, maybe you had a string that’s like "User not found for username {username}". | https://realpython.com/videos/string-format-method/#t=219.84 |
| 03:49 | https://realpython.com/videos/string-format-method/#t=229.53 |
| So then you’d just pass in a keyword argument and you’d say .format(username="Whatever") | https://realpython.com/videos/string-format-method/#t=229.53 |
| and it would nicely print this out for you. | https://realpython.com/videos/string-format-method/#t=238.11 |
| These keyword arguments can give you some nice semantic meaning within those | https://realpython.com/videos/string-format-method/#t=240.12 |
| strings so that the string is still readable | https://realpython.com/videos/string-format-method/#t=244.32 |
| even though you’re just putting in replacement fields. | https://realpython.com/videos/string-format-method/#t=246.6 |
| 04:09 | https://realpython.com/videos/string-format-method/#t=249.03 |
| If I hadn’t put a keyword argument and I’d made this a positional argument, | https://realpython.com/videos/string-format-method/#t=249.03 |
| it wouldn’t be quite as clear what this meant. But with a keyword argument, | https://realpython.com/videos/string-format-method/#t=251.91 |
| it can be. So, two important things to note here about these keywords and indices | https://realpython.com/videos/string-format-method/#t=255.66 |
| is, one, that you can’t mix indices with bare replacement fields. | https://realpython.com/videos/string-format-method/#t=260.64 |
| 04:25 | https://realpython.com/videos/string-format-method/#t=265.95 |
| So if I delete this 1, | https://realpython.com/videos/string-format-method/#t=265.95 |
| I’ll get a ValueError because the function can’t tell what’s supposed to go in | https://realpython.com/videos/string-format-method/#t=267.51 |
| here. Is it supposed to be the remaining index? | https://realpython.com/videos/string-format-method/#t=272.85 |
| 04:35 | https://realpython.com/videos/string-format-method/#t=275.82 |
| Is it supposed to be the index that’s at this order? So, is it supposed to be, | https://realpython.com/videos/string-format-method/#t=275.82 |
| "HOw are you"? Is it supposed to be "Hello"? It’s impossible to tell. | https://realpython.com/videos/string-format-method/#t=280.38 |
| 04:43 | https://realpython.com/videos/string-format-method/#t=283.89 |
| So that’s why they don’t allow the mixing of those two things. However, | https://realpython.com/videos/string-format-method/#t=283.89 |
| you can mix keyword and positional arguments. So if I say this, | https://realpython.com/videos/string-format-method/#t=287.28 |
| but then I say something like—well, let’s see. What if I change this to say | https://realpython.com/videos/string-format-method/#t=291.75 |
| first index here, and then I say last, | https://realpython.com/videos/string-format-method/#t=296.43 |
| and I make this last thing into a keyword argument with last. | https://realpython.com/videos/string-format-method/#t=300.84 |
| 05:05 | https://realpython.com/videos/string-format-method/#t=305.46 |
| Then that will work just fine, because those positional and keyword arguments are | https://realpython.com/videos/string-format-method/#t=305.46 |
| actually treated separately by Python in this function. So that’s nice to have. | https://realpython.com/videos/string-format-method/#t=309.33 |
| 05:15 | https://realpython.com/videos/string-format-method/#t=315.6 |
| And one thing that I almost forgot to note is that if you want to include | https://realpython.com/videos/string-format-method/#t=315.6 |
| braces, you can escape a brace by just doubling it. | https://realpython.com/videos/string-format-method/#t=319.26 |
| So this just means a literal brace character. | https://realpython.com/videos/string-format-method/#t=322.8 |
| 05:25 | https://realpython.com/videos/string-format-method/#t=325.98 |
| So then if I put three in a row, that means “Escape this brace and then put this | https://realpython.com/videos/string-format-method/#t=325.98 |
| thing in braces.” So again, I have to escape this second one, | https://realpython.com/videos/string-format-method/#t=330.09 |
| so three pairs of braces .format() | https://realpython.com/videos/string-format-method/#t=333.66 |
| and then I can put in whatever I like. I’ll put in "Liam" again, | https://realpython.com/videos/string-format-method/#t=337.47 |
| and as you can see, it puts '{Liam}' in braces. | https://realpython.com/videos/string-format-method/#t=340.23 |
| 05:42 | https://realpython.com/videos/string-format-method/#t=342.48 |
| This can be a little bit unwieldy if you want to put a lot of braces into | https://realpython.com/videos/string-format-method/#t=342.48 |
| some string that you’re using, but that’s kind of the price you pay— | https://realpython.com/videos/string-format-method/#t=347.07 |
| sometimes you just have to have that ability to use these braces as a nice | https://realpython.com/videos/string-format-method/#t=350.88 |
| format specifier. So now you know | https://realpython.com/videos/string-format-method/#t=355.41 |
| pretty much all the basic ways to use these replacement fields and the .format() | https://realpython.com/videos/string-format-method/#t=358.07 |
| method. Next up, | https://realpython.com/videos/string-format-method/#t=362.81 |
| I’ll go into a little more detail about the behavior and the specification of | https://realpython.com/videos/string-format-method/#t=364.7 |
| these replacement fields. | https://realpython.com/videos/string-format-method/#t=368.72 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/formatting-strings-overview/ |
| Overview | https://realpython.com/courses/formatting-python-strings/ |
| https://realpython.com/videos/name-and-conversion/ |
|
Formatting Python Strings (Overview) 00:44
| https://realpython.com/videos/formatting-strings-overview/ |
|
The Python String .format() Method 06:12
| https://realpython.com/videos/string-format-method/ |
|
The and Components 06:45
| https://realpython.com/videos/name-and-conversion/ |
|
The Component 09:19
| https://realpython.com/lessons/format-spec-component/ |
|
Nested Replacement Fields 04:07
| https://realpython.com/lessons/nested-replacement-fields/ |
|
Formatted String Literal (F-String) 05:08
| https://realpython.com/lessons/formatted-string-literal/ |
|
Formatting Python Strings (Quiz) 06:30
| https://realpython.com/lessons/formatting-python-strings-quiz/ |
|
Formatting Python Strings (Summary) 00:35
| https://realpython.com/lessons/formatting-strings-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover