All language subtitles for 3. String Formatting with Multiple Variables

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 Previously I introduced you how to dynamically insert the value of a variable inside a string. 1

So here we constructed "Hello" and whatever value the user entered in the input function. 2

So this worked with Python 2 and 3. 3

This works with Python 3.6 and later versions. Now what if you want to insert more than one value inside 4

a string? 5

How to do that? 6

For example let's say the first input function will ask us for the name and the second will ask us for 7

the surname, let's call this variable name. 8

And this one surname. To use this version of string formatting with two variables you'd need to do this: 9

Instead of just one variable, 10

you need to use round brackets and then enter all the variables you want to insert in the string. 11

Name and surname in this case. 12

Now if you execute this you're going to get an error. 13

So you need to do something more there. 14

Let's say Ardit Sulce and you got a type error. 15

Not all arguments converted during string formatting. 16

Which means that the arguments are Name and Surname. Not all of them were converted because only name 17

was converted because you only had one place holder here. 18

%s, you want to have two placeholders, one for surname too. 19

If you execute that, 20

well we get another error. 21

But that's not related to this line here. 22

This line is perfectly fine. 23

It works. 24

Why does it work? 25

Well, because you can see that the error happened, 26

so this is where the trace back starts. 27

You see that of the error happens in file basics.py which is the file where we have the code in 28

line 5 which means that above line 5 29

everything is OK. 30

So this line now has passed the error checking. 31

It's free of errors. So this is the line that has the error, and it's a name error, name user input is 32

not defined. 33

So this user inputs here has not been defined in our code. 34

We don't have a variable with that name. 35

We're just using it here but we didn't define it and here is a trick. 36

If you don't want to execute that line, you can comment it out. 37

,So by adding this symbol here, Python will ignore this line, it will treat it as a comment, not as code. 38

So save the script, execute, enter your input as a user. 39

And this is the output we get this time. 40

So that was a good exercise on how to fix errors. 41

Now back to our string formatting operations. 42

Let's see how this now will work with two variables. 43

So user inputs it's not a variable anymore. 44

We want to use name. 45

So f"Hello" {name}, and then we want to have a space here. 46

So this is a white space, so name whitespace {surname}, and it's as easy as that. 47

Execute, 48

and you get the output Hello 49

Ardit Sulce. 50

So that's the message variable, the string of the message variable. 51

So in this way you can put as many variables as you like. 52

For example: What's up {When}. 53

When could be a variable "today". 54

The string "today" is part of the when variable. 55

The when variable will be replaced in here by the value that it has. 56

It currently has. 57

Which is today. 58

Execute that 59

and that's how it works. 60

And that's what you need to know about string formatting 61

in Python.

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