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
Serbian
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
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.