Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Welcome back.
Let's talk about the topic of formatted strings.
Up until now we've just written simple strings but we want a program that's dynamic that's static.
Let's say we have a Amazon page and we're working on Amazon actually.
And when a user logs into their profile while we want to display their name or what they have in the
cart in that case we don't want to just hard code and write every single user's name like Andre or another
user Joe and write every single name in the world.
No.
Ideally what we can do is have something dynamic where let's say it's a profile page and we simply use
the name variable and we display that on the page.
Now this name variable should equal whatever the user's name is.
And this is something that we can graph from the database.
Again something that we'll talk later on in the course.
But let's assume that in here when we do equals we're going to grab some user information which will
be the name and this name for now will be John.
All right let's go Johnny and this Johnny.
We want to display on the profile page and we can do that with formatted strings we can simply do something
like print and then say name but we also want to greet that person.
Right.
So let's do hi and then plus name and remember to add a space if I click Run I get high.
Johnny but as we get more and more information let's say we have H and Johnny is fifty five.
If I wanted to extend the sentence and I want to say you are.
And then add another plus make sure we add a space in here and then add year old Ron here.
All right.
We get a type error must be string not INT.
Oh boy.
All right.
So we have to convert this into a string so remember we can do that like this.
And then if I click Run Hi Johnny.
You are a 55 year old or years old.
All right.
That was a little cumbersome but we're making our Amazon page dynamic we can grab different information
from the database and we'll have in a string something according to the user but there's a better way
of doing this.
And with formatted strings all we need to do is add an F. at the beginning in this f at the beginning
is going to tell Python hey this is going to be a format it's shrink and instead of doing all this plus
and name and all this stuff and doing the SDR to convert the type we can simply do something like this
Let's remove this and simply do brackets and say name and then again remove all of this
and say H.
So I'm going to say hi name you are eight years old and if I click Run here you see that it still works.
This is a new feature of Python 3 by adding F to the beginning.
It's saying hey this is going to be a formatted string and I want you to just make these variables available.
Strings inside of well this string how much cleaner is that now.
Although this is nice and clean and this is my preferred way of writing strings before Python 3.
You didn't really have this so in python 2 and mind you this works in Python 3 as well as you can see
we had something different to accomplish this what we had was this idea of a dot format and Dot format.
So let's remove the f here is going to do the same thing for us we do a bracket here and then we say
hey we want Johnny and age of 55 and we can just remove these variable names if I click run here.
I get an error and you have to be careful here.
Dot format works on strings.
You can see over here that we did it outside of the brackets and what it's doing is saying hey run the
format action on this print but print is not really a string right.
So you have to make sure that we move the brackets to the outside so that we evaluate this piece of
code first.
So the string is going to get formatted and then we're going to print if I run this hi Johnny you are
55 years old so that works the same way.
But what if we wanted to use variables because right now we're just doing this by order.
So whatever comes first gets filled in first in the brackets.
Well we can do something like name and age.
Let's see can we do that.
Let's run.
There you go.
That works the same way.
What if we had a specific order maybe we want this to be h in here to be name.
Well we can mix those around.
We can say 1 here and 0 here because in computer science we always start counting from zero.
So this is zero.
And this is one if we had something else in here let's say a third variable then this will be so if
I click Run here all right.
Everything is upside down.
Finally I can just create my own variables if I wanted to.
So let's say Hi let's say new name equals Sally and then age of Sally is going to equal let's say 100
Sally is very old.
Now if I do zero let's say I'm one here.
Quick run.
So now we'll actually get an error tuple index out of range.
Now we haven't really learned about tuples and ranges and this is the low confusing but a bit of a trick
here.
We want to make sure that we add now because we've given the actual variable a value.
We need to actually say new name here and here will be h so that if I click Run there you go.
Hi Sally.
You are a hundred years old as you can see with DOT format things are a little bit more complicated.
You'll still see this and all Python to code still uses the dot format and you'll see a lot of Python
3 code bases that still use dot format because some people prefer it but I would argue that the f at
the beginning of a formatted string is the way to go because well it just makes things so much easier.
Right.
Nice and clean nice and easy.
So I do recommend that you use the formatted string with the F in front.
And for short we usually call this an F string.
All right I'll see in the next one.
Bye bye.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.