All language subtitles for 7. String concatenation & interpolation--- [ FreeCourseWeb.com ] ---

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
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
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

In the previous exercise, we have seen how to print multiple variables to consult, but sometimes we

don't want to print things line by line and instead we want to combine variables together into a single

string.

For example, it would be nice if we could print a sentence that says, My name is Andrea, I'm 36 years

old and I'm one point eighty four meters tall.

So let's see how we can do this.

Using the variables that we have already defined by the way we can use single or double quotes to represent

a string literal.

So in this case, we will use double quotes.

So let's remove all these statements and then we can add print and then we then double quotes.

My name is and then we need to find a way to upend the first name variable to this string.

Now there are two ways of doing this, which are known as string concatenation and string interpolation.

So let's see how they work, starting with string concatenation.

Two concatenate two strings.

We can use the plus sign like this plus first name.

And if we run this code, we can see that it works just fine.

And because we can concatenate as many strings as we want here, we could add plus and then we then

double quotes a space so that we can separate the first name and the last name and then plus last name

like this.

And if we run this, the log will show both my name and surname.

But as you can see, this syntax is quite verbose and it quickly becomes hard to read if you have to

concatenate many strings to solve this problem that support string interpolation.

So let me show you how this works.

So here I can copy and paste this line and then I can replace all the concatenation code with the first

name and then space and then dollar last name and quotation mark at the end.

So when we insert a dollar followed by a variable inside a string, we are telling that to take the

value that that variable holds and insert it at this position in the string.

And if we run this code, we can see that the console shows my name is on the top two times.

And while both these print statements produce the same result, we can all agree that this syntax is

more concise and easier to read.

And when we write code, we should always aim to make it not just correct, but easy to understand.

And when creating strings from multiple variables, string interpolation gives us just a convenient

way of doing this so we can delete this line and here we can continue with our sentence and add.

I'm eight years old and I'm Dolarhyde metres tall.

And if we run this program now, we can see that we get the result that we want.

Now let's suppose that I want to print what my age will be next year.

To do that, I cannot.

A new print statement and this could say next year I will be the age plus one years old.

Now, if we run this code, the console will say that next year I will be thirty six plus one years

old.

And while this is technically correct, it is not exactly what we wanted to print.

So our goal here is to first take my age and then add one to it and then interpolate the result inside

this string.

But if we write code like this, that will only interpolate the variable that immediately follows the

dollar sign.

To get the result that we want, we can use a pair of curly braces to surround the expression that we

want to interpolate.

And as you can see, the syntax highlighter now shows age plus one with a different color to indicate

that it is an expression.

And now we can run this code and the result looks much better.

So the key takeaway here is that if you want to evaluate an expression inside a string, you can use

this syntax and put it inside curly braces.

After that, the last line, by the way, this is the first arithmetic expression that we see in this

course.

And we will talk about expressions more in detail a bit later.

Also, you may need to use curly braces in other cases.

For example, here we could define the current temperature of 30 degrees.

And I could print this by typing the LA Times like this.

And here I could add our space and then capital C for Celsius.

But if I wanted to print this without the space, the enterprise temp C as the name of a variable which

doesn't exist.

So to make this code work here and it is curly braces like this, so these examples should clarify how

you can use.

Interpellation with variables and expressions.

OK, so we can clear all this code and continue on the next lesson.

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