All language subtitles for 2. String Formatting

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian Download
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 How would you make a program that behaves like this? 1

It asks you for your name and you put your name as a user, and the program will greet you, wLL output 2

a message "Hello" and your name there, and also an exclamation mark at the end of the string. 3

Well you do that with two things. 4

First you need the input function to ask the user for input, to prompt the user for input and then you 5

need string formatting. 6

This is how string formatting looks like in Python. 7

So in the first line, we are getting the user input, storing that input as a string in user input. 8

Then we create a variable. In that variable, we will store the message we are constructing and this here 9

is a string formatting expression. 10

So we want to print out "Hello", and then have a space and then we want to print out the name of the user. 11

So the value that the user inputs. This here, %s is a special string in Python, then we have 12

an exclamation mark which is a normal string and the double quotes. 13

So this is special here. 14

And then after the string, you don't want to put a comma. What you want to do is use this percentage 15

operator and then use a variable. 16

Now the value of this variable will go and replace the %s. 17

So what you get instead is "Hello" 18

and your name there. 19

Now, this method of string formatting by using this %s here and percentage operator in here, 20

this is valid for Python 2 and Python 3 as well. 21

Now there is another method that was introduced in Python 3.6. 22

So the method I'll introduce now will not work if you run it with a Python version earlier than 23

3.6. 24

The method goes like this. 25

Let's store the entire string in a variable again and so it will be a string with double quotes or single 26

quotes whatever you prefer. 27

I'm using double quotes, so "Hello", and you use these curly brackets and inside that you put the name 28

of the variable which value you want to be put in this place. 29

Now this is not all. 30

So if I save these and execute the code and I say "Ardit", it will return the string. 31

So we need to do something else. 32

What we need to do is we need to add a prefix f just outside of the qoutes. 33

So this is the string. 34

This f goes outside of the quotes. 35

If I save the script now, execute again, write my name, and now we get the expected output, note that 36

we've got two variables with the same name here. 37

And what it will happen is that the first Python executes a script from top to bottom so it executed 38

the first line it executes the second line. 39

So it will store "Hello Ardit" in the message variable, but then we are overwriting that variable. 40

So in this case the string will be written in that message variable. 41

So whatever you create in here, whatever object you store in here, in the variable that object will be 42

the current object that that variable will refer to. 43

So in this case here you will print out the object that you created in here which happens to be "Hello Ardit". 44

Which one should you use? 45

Well, if you think that your project is going to be run with Python 3.6 or above, then use this 46

nicer version, it's more readable, if you think that your project, your python files will be run with 47

Python 2 or 3.1, or 3.2, or 3.3, then you might want to opt in for this method because sometimes 48

when you deploy your scripts in a server for example let's say you want to execute a script every day 49

at the certain hour, you want to deploy it on a web server, and that's web server will have a specific 50

version of Python. 51

Sometimes that version is Python 2. 52

So in that case you want to adjust your scripts so that it works with Python 2 and the web server, 53

the server, the computer will run your script with a particular Python version. 54

We're going to look at this later when we deploy the web apps. 55

So for now these are the two methods of string formatting.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.