All language subtitles for 3. String Formatting with Multiple Variables

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian Download
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,420 --> 00:00:07,340 Previously I introduced you how to dynamically insert the value of a variable inside a string. 1 2 00:00:07,380 --> 00:00:15,000 So here we constructed "Hello" and whatever value the user entered in the input function. 2 3 00:00:15,180 --> 00:00:18,310 So this worked with Python 2 and 3. 3 4 00:00:18,360 --> 00:00:26,380 This works with Python 3.6 and later versions. Now what if you want to insert more than one value inside 4 5 00:00:26,390 --> 00:00:27,050 a string? 5 6 00:00:27,060 --> 00:00:28,110 How to do that? 6 7 00:00:28,170 --> 00:00:34,470 For example let's say the first input function will ask us for the name and the second will ask us for 7 8 00:00:34,470 --> 00:00:39,460 the surname, let's call this variable name. 8 9 00:00:39,460 --> 00:00:47,890 And this one surname. To use this version of string formatting with two variables you'd need to do this: 9 10 00:00:48,130 --> 00:00:49,630 Instead of just one variable, 10 11 00:00:49,630 --> 00:00:57,310 you need to use round brackets and then enter all the variables you want to insert in the string. 11 12 00:00:57,340 --> 00:01:00,580 Name and surname in this case. 12 13 00:01:00,580 --> 00:01:03,660 Now if you execute this you're going to get an error. 13 14 00:01:03,790 --> 00:01:05,550 So you need to do something more there. 14 15 00:01:05,590 --> 00:01:10,710 Let's say Ardit Sulce and you got a type error. 15 16 00:01:10,720 --> 00:01:14,230 Not all arguments converted during string formatting. 16 17 00:01:14,380 --> 00:01:22,690 Which means that the arguments are Name and Surname. Not all of them were converted because only name 17 18 00:01:22,690 --> 00:01:26,090 was converted because you only had one place holder here. 18 19 00:01:26,140 --> 00:01:31,570 %s, you want to have two placeholders, one for surname too. 19 20 00:01:32,280 --> 00:01:33,130 If you execute that, 20 21 00:01:36,180 --> 00:01:37,360 well we get another error. 21 22 00:01:37,360 --> 00:01:41,500 But that's not related to this line here. 22 23 00:01:41,500 --> 00:01:43,360 This line is perfectly fine. 23 24 00:01:43,360 --> 00:01:44,400 It works. 24 25 00:01:44,470 --> 00:01:45,940 Why does it work? 25 26 00:01:45,940 --> 00:01:49,570 Well, because you can see that the error happened, 26 27 00:01:49,600 --> 00:01:52,340 so this is where the trace back starts. 27 28 00:01:52,480 --> 00:01:59,130 You see that of the error happens in file basics.py which is the file where we have the code in 28 29 00:01:59,120 --> 00:02:02,680 line 5 which means that above line 5 29 30 00:02:02,680 --> 00:02:04,470 everything is OK. 30 31 00:02:04,570 --> 00:02:08,440 So this line now has passed the error checking. 31 32 00:02:08,440 --> 00:02:16,450 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 33 00:02:16,470 --> 00:02:17,350 not defined. 33 34 00:02:17,730 --> 00:02:22,540 So this user inputs here has not been defined in our code. 34 35 00:02:22,590 --> 00:02:25,180 We don't have a variable with that name. 35 36 00:02:25,200 --> 00:02:28,850 We're just using it here but we didn't define it and here is a trick. 36 37 00:02:28,860 --> 00:02:33,970 If you don't want to execute that line, you can comment it out. 37 38 00:02:34,080 --> 00:02:45,770 ,So by adding this symbol here, Python will ignore this line, it will treat it as a comment, not as code. 38 39 00:02:45,870 --> 00:02:52,560 So save the script, execute, enter your input as a user. 39 40 00:02:52,580 --> 00:02:54,600 And this is the output we get this time. 40 41 00:02:54,600 --> 00:03:00,700 So that was a good exercise on how to fix errors. 41 42 00:03:00,700 --> 00:03:03,800 Now back to our string formatting operations. 42 43 00:03:03,880 --> 00:03:07,930 Let's see how this now will work with two variables. 43 44 00:03:07,930 --> 00:03:10,840 So user inputs it's not a variable anymore. 44 45 00:03:10,840 --> 00:03:12,940 We want to use name. 45 46 00:03:12,940 --> 00:03:17,830 So f"Hello" {name}, and then we want to have a space here. 46 47 00:03:17,830 --> 00:03:26,550 So this is a white space, so name whitespace {surname}, and it's as easy as that. 47 48 00:03:27,130 --> 00:03:27,800 Execute, 48 49 00:03:31,030 --> 00:03:32,850 and you get the output Hello 49 50 00:03:33,150 --> 00:03:34,830 Ardit Sulce. 50 51 00:03:34,930 --> 00:03:38,390 So that's the message variable, the string of the message variable. 51 52 00:03:38,620 --> 00:03:44,450 So in this way you can put as many variables as you like. 52 53 00:03:45,010 --> 00:03:48,820 For example: What's up {When}. 53 54 00:03:49,210 --> 00:03:53,250 When could be a variable "today". 54 55 00:03:53,280 --> 00:03:56,230 The string "today" is part of the when variable. 55 56 00:03:56,230 --> 00:04:01,060 The when variable will be replaced in here by the value that it has. 56 57 00:04:01,090 --> 00:04:02,020 It currently has. 57 58 00:04:02,650 --> 00:04:03,680 Which is today. 58 59 00:04:04,000 --> 00:04:04,660 Execute that 59 60 00:04:08,050 --> 00:04:09,750 and that's how it works. 60 61 00:04:10,540 --> 00:04:13,240 And that's what you need to know about string formatting 61 62 00:04:13,270 --> 00:04:14,080 in Python. 5611

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