Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
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
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.