Formatting Strings | Python for Beginners [11 of 44]

>>So previously, we
saw how we could take a couple of strings and combine them together by using
that literal plus operator. Now, we’ve already seen
this slide and if you take a look at the code in
particular that fourth line, what you’re going to notice
there is there’s a lot going on, that I’ve got that little plus
right there in the middle, with my string literal, and then I’ve got another plus here, and another string literal, and another plus here, and another string literal. This could get on
weirdly pretty quickly, and this is not even taking into account that we might
want to be calling capitalize, or upper, or lower, or some of those other helper
functions that we might have, and our codes are just going
to keep getting longer, and longer, and longer. So let’s try and simplify
this a little bit. This is where our format
strings come into play. Now, what we can do is that little
output that we see up at the top where again we’re calling that plus sign that
we’ve already seen, or we can streamline this
by using placeholders. Now, each one of these outputs that you see up here on the slide is going to give us
the exact same string. We’re just going to do
it slightly differently. So the first one, what we’re doing here
is we’re going in and we’re putting in place holders
with those curly braces. Now, the way that this works
is it’s going to be based on the order in which we
specify the parameters. So that first one there, that’s going to be
first name in my case, and that second one, that is going to be last name. Now, if we wanted to specify it, what we can do instead
is we can use the zero, and the one which then allows us to specify the first and
the second respectively. Remember, that counting
will start with zero, so that zero is going
to be the first item, and the one is going
to be the second item. Now, in my example up here, it’s not going to make a difference, that both of them are the exact same. But if I need to potentially reuse the exact same string somewhere else or maybe I just want to
document it, show hey, this is going to be the first, this
is going to be for the second, this is going to be for the third, then I could go ahead
and put in that zero, the one, and then maybe
a two later on as well. Now, the last example that
I want to highlight here, and I want to make sure
that I point out the fact, that was not what I wanted, I wanted that, there we go. I want to make sure that I
point out the fact that this is only available in Python 3. So if you’re doing anything
that needs to run in Python 2, this last example is
not going to work, but it will work in Python 3. That is where I put in f
right at the very beginning, f being for format, and now what I’m able to do
and I love this functionality, is I’m able to now just use my variable names right in
line with everything else. This is my preferred method whenever I’m doing
string concatenation, because it’s nice, it’s
clear, it’s self-documenting, you always want your code
to be self-documenting, and when somebody comes
back assuming that they understand the little
f at the beginning, it’s very easy for them to go, “Oh, that’s going to be my first name, and that is going to
be my last name.” Let’s see how all of this works inside of code in
our next little video.

10 thoughts on “Formatting Strings | Python for Beginners [11 of 44]

  1. This is an alternative to using %s ryt?
    For eg:
    (name, age) = ('xyz', 20)
    sent = 'Hello %s, of age %d'
    print(sent%(name, age))

  2. Thanks for pulling these together. Viewers: Note that "output" is a variable that must be printed using print(), and not (as might be interpreted by newbies, like me) an intrinsic Python function.

  3. No output for the below code :

    my_name=input('What is your Name: ')
    my_sirname=input('What is your Sirname: ')
    output= 'Hello, {0} {1}'.format(my_name, my_sirname)
    output=f'Hello, {my_name} {my_sirname}'

  4. I am listening to the tutorial and I really appreciate the contents, but Christopher is using his voice at times in a very high pitch (non bass) way that makes it difficult to listen to for long. The woman's voice has a lower note than him, it's the speaking style. So Christopher, if possible for future recordings can you bear that in mind?

  5. In older tutorials on YouTube, you will also see people doing this:
    output = 'Hello, %s %s' % (first_name, last_name)

    "%s" means string, you have two strings first name and last name. And to determine their order you write "% (first_name, last_name)". It is important to know though that this is discouraged nowadays. I'm only mentioning it here, in case you come across any other tutorial which doesn't use any of the upper methods.

    It is also useful to know that using the + method (concatenation) or the f'Text{} {}' (f-strings) are the fastest ones. Lastly, the f-strings only work in Python 3.6 and later versions.

Leave a Reply

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