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 How would you make a program that behaves like this? 1
It asks you for your name and you put your name as a user, and the program will greet you, wLL output 2
a message "Hello" and your name there, and also an exclamation mark at the end of the string. 3
Well you do that with two things. 4
First you need the input function to ask the user for input, to prompt the user for input and then you 5
need string formatting. 6
This is how string formatting looks like in Python. 7
So in the first line, we are getting the user input, storing that input as a string in user input. 8
Then we create a variable. In that variable, we will store the message we are constructing and this here 9
is a string formatting expression. 10
So we want to print out "Hello", and then have a space and then we want to print out the name of the user. 11
So the value that the user inputs. This here, %s is a special string in Python, then we have 12
an exclamation mark which is a normal string and the double quotes. 13
So this is special here. 14
And then after the string, you don't want to put a comma. What you want to do is use this percentage 15
operator and then use a variable. 16
Now the value of this variable will go and replace the %s. 17
So what you get instead is "Hello" 18
and your name there. 19
Now, this method of string formatting by using this %s here and percentage operator in here, 20
this is valid for Python 2 and Python 3 as well. 21
Now there is another method that was introduced in Python 3.6. 22
So the method I'll introduce now will not work if you run it with a Python version earlier than 23
3.6. 24
The method goes like this. 25
Let's store the entire string in a variable again and so it will be a string with double quotes or single 26
quotes whatever you prefer. 27
I'm using double quotes, so "Hello", and you use these curly brackets and inside that you put the name 28
of the variable which value you want to be put in this place. 29
Now this is not all. 30
So if I save these and execute the code and I say "Ardit", it will return the string. 31
So we need to do something else. 32
What we need to do is we need to add a prefix f just outside of the qoutes. 33
So this is the string. 34
This f goes outside of the quotes. 35
If I save the script now, execute again, write my name, and now we get the expected output, note that 36
we've got two variables with the same name here. 37
And what it will happen is that the first Python executes a script from top to bottom so it executed 38
the first line it executes the second line. 39
So it will store "Hello Ardit" in the message variable, but then we are overwriting that variable. 40
So in this case the string will be written in that message variable. 41
So whatever you create in here, whatever object you store in here, in the variable that object will be 42
the current object that that variable will refer to. 43
So in this case here you will print out the object that you created in here which happens to be "Hello Ardit". 44
Which one should you use? 45
Well, if you think that your project is going to be run with Python 3.6 or above, then use this 46
nicer version, it's more readable, if you think that your project, your python files will be run with 47
Python 2 or 3.1, or 3.2, or 3.3, then you might want to opt in for this method because sometimes 48
when you deploy your scripts in a server for example let's say you want to execute a script every day 49
at the certain hour, you want to deploy it on a web server, and that's web server will have a specific 50
version of Python. 51
Sometimes that version is Python 2. 52
So in that case you want to adjust your scripts so that it works with Python 2 and the web server, 53
the server, the computer will run your script with a particular Python version. 54
We're going to look at this later when we deploy the web apps. 55
So for now these are the two methods of string formatting.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.