1
00:00:01,260 --> 00:00:06,420
How would you make a program that behaves like this?

2
00:00:06,450 --> 00:00:13,500
It asks you for your name and you put your name as a user, and the program will greet you, wLL output

3
00:00:13,560 --> 00:00:19,650
a message "Hello" and your name there, and also an exclamation mark at the end of the string.

4
00:00:19,650 --> 00:00:21,650
Well you do that with two things.

5
00:00:21,660 --> 00:00:28,440
First you need the input function to ask the user for input,
to prompt the user for input and then you

6
00:00:28,440 --> 00:00:31,230
need string formatting.

7
00:00:31,230 --> 00:00:33,830
This is how string formatting looks like in Python.

8
00:00:35,020 --> 00:00:41,060
So in the first line, we are getting the user input, storing that input as a string in user input.

9
00:00:41,150 --> 00:00:48,620
Then we create a variable. In that variable, we will store the message we are constructing and this here

10
00:00:48,640 --> 00:00:51,210
is a string formatting expression.

11
00:00:51,340 --> 00:01:00,160
So we want to print out "Hello", and then have a space and then we want to print out the name of the user.

12
00:01:00,160 --> 00:01:09,000
So the value that the user inputs. This here, %s is a special string in Python, then we have

13
00:01:09,000 --> 00:01:13,950
an exclamation mark which is a normal string and the double quotes.

14
00:01:14,070 --> 00:01:15,630
So this is special here.

15
00:01:16,260 --> 00:01:23,670
And then after the string, you don't want to put a comma.
What you want to do is use this percentage

16
00:01:23,730 --> 00:01:27,150
operator and then use a variable.

17
00:01:27,160 --> 00:01:33,450
Now the value of this variable will go and replace the %s.

18
00:01:33,500 --> 00:01:35,620
So what you get instead is "Hello"

19
00:01:35,670 --> 00:01:37,140
and your name there.

20
00:01:37,170 --> 00:01:43,980
Now, this method of string formatting by using this %s here and percentage operator in here,

21
00:01:44,230 --> 00:01:49,200
this is valid for Python 2 and Python 3 as well.

22
00:01:49,200 --> 00:01:53,680
Now there is another method that was introduced in Python 3.6.

23
00:01:53,940 --> 00:02:00,060
So the method I'll introduce now will not work if you run it with a Python version earlier than

24
00:02:00,060 --> 00:02:01,500
3.6.

25
00:02:01,500 --> 00:02:04,740
The method goes like this.

26
00:02:04,740 --> 00:02:12,750
Let's store the entire string in a variable again and so it will be a string with double quotes or single

27
00:02:12,750 --> 00:02:14,680
quotes whatever you prefer.

28
00:02:14,670 --> 00:02:22,770
I'm using double quotes, so "Hello", and you use these curly brackets and inside that you put the name

29
00:02:22,800 --> 00:02:31,350
of the variable which value you want to be put in this place.

30
00:02:31,350 --> 00:02:32,910
Now this is not all.

31
00:02:32,910 --> 00:02:40,400
So if I save these and execute the code and I say "Ardit", it will return the string.

32
00:02:40,400 --> 00:02:41,770
So we need to do something else.

33
00:02:42,030 --> 00:02:47,490
What we need to do is we need to add a prefix f just outside of the qoutes.

34
00:02:47,550 --> 00:02:48,820
So this is the string.

35
00:02:48,900 --> 00:02:51,240
This f goes outside of the quotes.

36
00:02:51,660 --> 00:03:00,950
If I save the script now, execute again, write my name, and now we get the expected output, note that

37
00:03:01,610 --> 00:03:04,250
we've got two variables with the same name here.

38
00:03:04,610 --> 00:03:09,950
And what it will happen is that the first Python executes a script from top to bottom so it executed

39
00:03:09,950 --> 00:03:12,560
the first line it executes the second line.

40
00:03:12,560 --> 00:03:23,450
So it will store "Hello Ardit" in the message variable, but then we are overwriting that variable.

41
00:03:23,450 --> 00:03:28,490
So in this case the string will be written in that message variable.

42
00:03:28,490 --> 00:03:33,530
So whatever you create in here, whatever object you store in here, in the variable that object will be

43
00:03:33,530 --> 00:03:37,550
the current object that that variable will refer to.

44
00:03:37,550 --> 00:03:43,430
So in this case here you will print out the object that you created in here which happens to be "Hello Ardit".

45
00:03:45,480 --> 00:03:46,720
Which one should you use?

46
00:03:46,740 --> 00:03:55,260
Well, if you think that your project is going to be run with Python 3.6 or above, then use this

47
00:03:55,650 --> 00:04:02,310
nicer version, it's more readable, if you think that your project, your python files will be run with

48
00:04:02,400 --> 00:04:09,660
Python 2 or 3.1, or 3.2, or 3.3, then you might want to opt in for this method because sometimes

49
00:04:09,660 --> 00:04:16,320
when you deploy your scripts in a server for example let's say you want to execute a script every day

50
00:04:16,350 --> 00:04:23,160
at the certain hour, you want to deploy it on a web server, and that's web server will have a specific

51
00:04:23,160 --> 00:04:24,700
version of Python.

52
00:04:24,720 --> 00:04:27,700
Sometimes that version is Python 2.

53
00:04:27,750 --> 00:04:34,890
So in that case you want to adjust your scripts so that it works with Python 2 and the web server,

54
00:04:34,890 --> 00:04:40,440
the server, the computer will run your script with a particular Python version.

55
00:04:40,440 --> 00:04:44,130
We're going to look at this later when we deploy the web apps.

56
00:04:44,550 --> 00:04:47,540
So for now these are the two methods of string formatting.

