René's URL Explorer Experiment


Title: The Python String .format() Method (Video) – Real Python

Open Graph Title: The Python String .format() Method – Real Python

Description: In this lesson, I’ll show you the basics of how the string .format() method works. When I talk about string formatting, what I really mean is you need to have some method for interpolating Python values into pre-built strings. And by interpolation…

Open Graph Description: In this lesson, I’ll show you the basics of how the string .format() method works. When I talk about string formatting, what I really mean is you need to have some method for interpolating Python values into pre-built strings. And by interpolation…

Opengraph URL: https://realpython.com/videos/string-format-method/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "The Python String .format() Method",
    "description": "In this lesson, I’ll show you the basics of how the string .format() method works. When I talk about string formatting, what I really mean is you need to have some method for interpolating Python values into pre\u002Dbuilt strings. And by interpolation…",
    "thumbnailUrl": ["https://files.realpython.com/media/A-Guide-to-the-Newer-Python-String-Format-Techniques_Watermarked.52c033817b79.jpg"],
    "uploadDate": "2020-11-24T14:00:00+00:00",
    "duration": "PT6M12S",
    "embedUrl": "https://player.vimeo.com/video/475622779",
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/videos/string-format-method/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/A-Guide-to-the-Newer-Python-String-Format-Techniques_Watermarked.52c033817b79.jpg
og:imagehttps://files.realpython.com/media/A-Guide-to-the-Newer-Python-String-Format-Techniques_Watermarked.52c033817b79.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/videos/string-format-method/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/videos/string-format-method/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Fvideos%2Fstring-format-method%2F
https://realpython.com/courses/formatting-python-strings/#team
Formatting Python Stringshttps://realpython.com/courses/formatting-python-strings/
Liam Pulsiferhttps://realpython.com/courses/formatting-python-strings/#team
Recommended Tutorialhttps://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 Questionhttps://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
Transcripthttps://realpython.com/videos/string-format-method/#transcript
Discussionhttps://realpython.com/videos/string-format-method/#discussion
00:00https://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:07https://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 Pythonhttps://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 ahttps://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:27https://realpython.com/videos/string-format-method/#t=27.74
This is useful for creating dynamic text—that is, texthttps://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:36https://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 ofhttps://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:46https://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 thosehttps://realpython.com/videos/string-format-method/#t=65.78
things inside the braces.https://realpython.com/videos/string-format-method/#t=69.83
01:11https://realpython.com/videos/string-format-method/#t=71.81
But those braces’ replacement fields are substituted for by the arguments to thehttps://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 tohttps://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:27https://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 athttps://realpython.com/videos/string-format-method/#t=99.29
this memory location.https://realpython.com/videos/string-format-method/#t=104.18
01:45https://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:53https://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:03https://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 tohttps://realpython.com/videos/string-format-method/#t=127.73
you could make this into a nice function that would just take in a name andhttps://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:17https://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 fieldshttps://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 thosehttps://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 thehttps://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:15https://realpython.com/videos/string-format-method/#t=195.96
b, and c—https://realpython.com/videos/string-format-method/#t=195.96
03:19https://realpython.com/videos/string-format-method/#t=199.68
as long as your actual arguments contain those three different keywordshttps://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 likehttps://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:36https://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:49https://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 thosehttps://realpython.com/videos/string-format-method/#t=240.12
strings so that the string is still readablehttps://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:09https://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 indiceshttps://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:25https://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 inhttps://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:35https://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:43https://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 sayhttps://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:05https://realpython.com/videos/string-format-method/#t=305.46
Then that will work just fine, because those positional and keyword arguments arehttps://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:15https://realpython.com/videos/string-format-method/#t=315.6
And one thing that I almost forgot to note is that if you want to includehttps://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:25https://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 thishttps://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:42https://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 intohttps://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 nicehttps://realpython.com/videos/string-format-method/#t=350.88
format specifier. So now you knowhttps://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 ofhttps://realpython.com/videos/string-format-method/#t=364.7
these replacement fields.https://realpython.com/videos/string-format-method/#t=368.72
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/videos/formatting-strings-overview/
Overviewhttps://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 Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.