Demo: Formatting Strings | Python for Beginners [12 of 44]


>>Let’s see how we can
do that string format. So I’m going to start off
by doing first_name equals Christopher and last_name equals
Harrison, just like before. Again, what we could do is, we could just simply
output equals our “Hello” and our little
plus and our first_name, a little plus, a space, and then hello and then our
last_name at the end there. There we go, and then I
say print and our output, and if I run this
correctly, there we go. That will give us
our little Hello Christopher. Now, I’m going to do
the exact same thing multiple times and we should
just get the exact same output. My goal here is really just to show the different ways
that we can do this. So let me comment that out, and now, let’s do this again by using
the little placeholders. So we’ll go ahead and say, “Hello”, and then we’ll just do two curlies, and then two curlies, and now I’ll say format. Again, using tab to give
me that completion there, and I’ll say first_name and
last_name, there we go. Let’s go ahead and
save that, rerun it, once again, the exact same output,
Hello Christopher Harrison. If I want to maybe be
specific about it, so let’s go in and say 1 and
0, and go ahead and run it. Now, you’re going to notice
we’ll get the exact same output, but maybe if I wanted to reverse
that last_name and first_name, maybe I want to put
a comma between them, so I’ll go in and I do 1 and then 0, put a comma in the middle. Now, what you’re going to
notice when I run this is that, it went ahead and gave
me Hello Harrison, Christopher, without me having to change the order of the parameters, which is another advantage to going
in and specifying the numbers. Now again, my preferred mechanism is just to use that cool
little format string. So I’m going to say output equals
and then we’ll go F, and now I’ll say, Hello, curly, and then
we’ll say first_name, space, curly, last_name, and you’ll notice
the intellisense actually comes right along
for the right there, suggesting first_name
and last_name for me, and now I’ll go ahead and hit “Save”, and I rerun that, and now we’re back to that
Hello, Christopher Harrison. So the big takeaway
that I want you to get out of all of
those examples is that there are multiple ways
that you can do the exact same types of
string concatenation. It’s really up to you to decide
which way it is that you prefer. For me, like I said previously, I really like that last string format with the little f at
the very beginning there. That to me reads the best, and you’ll also notice
that there’s a lot of other programming
languages that have a very similar construct such as in, say, JavaScript or in
C sharp, for example. I also really like the fact
that it is self-documenting, because I can very clearly see, oh, that’s where first_name is going to be and that’s where
last_name is going to be. That’s how we can work with
strings inside of Python.

14 thoughts on “Demo: Formatting Strings | Python for Beginners [12 of 44]

  1. I'm using python 3.7.4 and I can't use the last method, with the f at the beginning. Why is that? Console throws a syntax error

  2. please what do i do or type when my code only runs on the shell for a short period of time and closes afterwards

  3. my issue was I was leaving a space after the f, see examples below.

    output =f 'Hello, {first_name} {last_name}'

    print (output)

    SyntaxError: invalid syntax

    output = f'Hello, {first_name} {last_name}'

    print (output)

    Hello, John Doe

  4. So, f and .format are ALWAYS initial caps with a space between outputs? How would you designate other formats, like ALL CAPS or all lower case?

Leave a Reply

Your email address will not be published. Required fields are marked *