1
00:00:04,960 --> 00:00:08,980
In this video, I want to discuss strings in a bit more detail,

2
00:00:08,980 --> 00:00:13,240
and also introduce the idea of storing values in variables.

3
00:00:13,240 --> 00:00:17,400
We'll cover variables in the video after next, but I'll actually introduce them here.

4
00:00:17,400 --> 00:00:22,800
So we'll continue using our HelloWorld project, but let's create a new Python file for this video.

5
00:00:22,800 --> 00:00:25,720
So we're going to open our project pane.

6
00:00:25,720 --> 00:00:28,320
I'm gonna right click on the project name,

7
00:00:28,320 --> 00:00:32,860
click on New and choose Python File,

8
00:00:32,860 --> 00:00:36,840
and I'll call this one strings.

9
00:00:36,840 --> 00:00:42,040
And I'll just close the project pane by double clicking on the file name here, strings.py,

10
00:00:42,040 --> 00:00:44,000
so we can see what we're doing.

11
00:00:44,000 --> 00:00:49,200
Now in Python, strings can be enclosed in either double or single quotes, as I've mentioned previously.

12
00:00:49,200 --> 00:00:53,520
Let's just go through and type in a couple of examples, noting that these are all valid

13
00:00:53,520 --> 00:01:01,180
Python strings. So the first one we're going to
start with, "Today is a good day to learn Python".

14
00:01:01,180 --> 00:01:08,120
You can see that's valid. Next line, using single quotes, "Python is fun".

15
00:01:08,120 --> 00:01:19,540
Next line, print double quotes Python
single quote s strings are easy to use,

16
00:01:19,540 --> 00:01:23,160
and on line 4 I'm going to type print
parentheses again,

17
00:01:23,160 --> 00:01:35,680
single quote, We can even include double quotes, quotes, in strings. Alright.

18
00:01:35,680 --> 00:01:42,620
So unlike some other languages like Perl, there's no difference between single or double quotes in Python.

19
00:01:42,620 --> 00:01:46,700
However, if you start a string with one type then you must finish it with the same type, and

20
00:01:46,700 --> 00:01:48,280
we've talked about that previously.

21
00:01:48,280 --> 00:01:53,580
If you need to include a single quote in a string, then use double quotes to enclose the string,

22
00:01:53,580 --> 00:01:55,560
like in the third example above.

23
00:01:55,560 --> 00:01:56,920
And similarly,

24
00:01:56,920 --> 00:02:03,240
if your string will include double quotes, then enclose the string in single quotes, as we've done on line 4.

25
00:02:03,240 --> 00:02:08,720
Now we can also concatenate strings to make longer ones, using plus.

26
00:02:08,720 --> 00:02:17,120
Let's go ahead and type print parentheses double quotes hello +

27
00:02:17,120 --> 00:02:20,700
double quotes again, world with a space at the start there.

28
00:02:20,700 --> 00:02:25,440
So that joins the two words together, to make the single string, hello world.

29
00:02:25,440 --> 00:02:27,940
So that's called string concatenation.

30
00:02:27,940 --> 00:02:34,200
Python isn't going to perform addition on the two strings. It just joins them together or concatenates them.

31
00:02:34,200 --> 00:02:37,200
So I'm gonna run the program to check that it prints what we expect.

32
00:02:37,200 --> 00:02:40,340
But remember to check the drop-down in the top right hand corner,

33
00:02:40,340 --> 00:02:46,820
and right-clicking the editor to run the correct program, otherwise you'll run helloworld.py by mistake.

34
00:02:46,820 --> 00:02:49,780
So if we do that without changing that, and run it,

35
00:02:49,780 --> 00:02:54,860
note that we're getting the Hello World output, and not the program output from this program,

36
00:02:54,860 --> 00:02:59,300
which could be a bit confusing for you. So you want to right-click in here instead,

37
00:02:59,300 --> 00:03:01,320
click on run the first time.

38
00:03:01,320 --> 00:03:05,360
By doing that, we're now running this program, and noting that in the top right hand corner,

39
00:03:05,360 --> 00:03:11,760
the drop down has now, or is now showing strings, being the current program that we're running.

40
00:03:11,760 --> 00:03:14,160
And you can see that in the case of line 5,

41
00:03:14,160 --> 00:03:18,560
we've got hello world showing - the
two strings concatenated together to form one string -

42
00:03:18,560 --> 00:03:23,740
and we've got the single quotes and double quotes showing, as per the examples on lines 3 and 4,

43
00:03:23,740 --> 00:03:27,160
and the rest of the text, as per what we've set up.

44
00:03:27,160 --> 00:03:33,440
Alright, so we can also store the strings in variables, and then concatenation makes more sense.

45
00:03:33,440 --> 00:03:35,340
So let's go ahead and do that.

46
00:03:35,340 --> 00:03:37,420
So we can come down here,

47
00:03:37,420 --> 00:03:50,860
and I can type greeting equals Hello in double quotes, name equals, we'll type Bruce in double quotes.

48
00:03:50,860 --> 00:04:00,740
I'm gonna put a comment here, hash, if we want a space, we can add that too.

49
00:04:00,740 --> 00:04:06,420
And we type print left and right parenthesis, and within the parentheses we can type greeting,

50
00:04:06,420 --> 00:04:12,300
noting that I've just typed a g and IntelliJ helpfully gives us a menu that we can select from, and if we've got the

51
00:04:12,300 --> 00:04:16,040
menu options selected that we want, we
can use the up and down arrow keys, by the way.

52
00:04:16,040 --> 00:04:19,779
But greeting is what we want. I'm going to press ENTER there. That saves us a bit of typing.

53
00:04:19,779 --> 00:04:28,160
So I can then add + single quote with a space in it , + I can put it in n for name, press ENTER,

54
00:04:28,160 --> 00:04:31,900
and if we run the program now,

55
00:04:31,900 --> 00:04:35,440
we can see the strings have been concatenated to produce the output.

56
00:04:35,440 --> 00:04:41,640
So to make this even more useful, we could use the input function to read in a name from the keyboard.

57
00:04:41,640 --> 00:04:43,360
So let's go ahead and do that.

58
00:04:43,360 --> 00:04:48,300
So where we've got name defined on line 7, let's change that to be name equals,

59
00:04:48,300 --> 00:04:51,480
and we'll delete the text Bruce in double quotes

60
00:04:51,480 --> 00:04:59,920
and instead we'll make an input parenthesis double quotes, Please enter your name, and a space.

61
00:04:59,920 --> 00:05:03,800
Then our closing double quote and right parenthesis.

62
00:05:03,800 --> 00:05:09,240
The input function displays the text provided to it, then waits for text to be entered at the keyboard.

63
00:05:09,240 --> 00:05:13,160
So I'm going to right click and run the program.

64
00:05:13,160 --> 00:05:17,080
Now when you run the program, click into the run window with the mouse,

65
00:05:17,080 --> 00:05:20,800
or you can use alt 4 on a PC before typing your name.

66
00:05:20,800 --> 00:05:23,120
So I'm going to come into here and I'm just going to double click it.

67
00:05:23,120 --> 00:05:25,260
I'm going to type Tim with a capital T.

68
00:05:25,260 --> 00:05:29,700
When you press Enter, the text you typed
is stored in the variable name.

69
00:05:29,700 --> 00:05:33,700
This is the code on line 7 and then used by the print function to show the greeting.

70
00:05:33,700 --> 00:05:38,760
So if we press ENTER, we can now see the output, Hello Tim.

71
00:05:38,760 --> 00:05:44,620
So that's pretty cool. Our programs can get input from the user. To do that we call the input function,

72
00:05:44,620 --> 00:05:48,860
and then assign the value it returns to
a variable.

73
00:05:48,860 --> 00:05:53,000
I mentioned function return values, in the slide at the end of the last video,

74
00:05:53,000 --> 00:05:58,740
so here the return value of the input function, is whatever text the user enters.

75
00:05:58,740 --> 00:06:02,700
So we assign that to the variable,
name on line 7.

76
00:06:02,700 --> 00:06:09,700
Assignment is done using the equals symbol, and anything on the right of the equals symbol is evaluated first,

77
00:06:09,700 --> 00:06:11,700
before the assignment happens.

78
00:06:11,700 --> 00:06:17,320
So looking at line 7, that means the input function is called first.

79
00:06:17,320 --> 00:06:20,900
When it finishes, which happens when the user presses enter,

80
00:06:20,900 --> 00:06:25,900
the value it returns is assigned to our variable name.

81
00:06:25,900 --> 00:06:30,800
So let's actually come in here and type in print below the input line, print greeting,

82
00:06:30,800 --> 00:06:35,980
I probably should have done this before,
greeting + name,

83
00:06:35,980 --> 00:06:42,240
because I've added the space down here on line 11, but I didn't do it on line 8 to show you what it's like without the space.

84
00:06:42,240 --> 00:06:45,300
So let's run that again.

85
00:06:45,300 --> 00:06:54,100
Click into the run window, Tim, and you see HelloTim without the space, and also Hello Tim with the space.

86
00:06:54,100 --> 00:06:57,720
So as you can see now, on lines 8 and 11,

87
00:06:57,720 --> 00:06:59,740
the value of name is printed out.

88
00:06:59,740 --> 00:07:04,200
Name isn't enclosed in speech marks, and again that's because it's not a string literal.

89
00:07:04,200 --> 00:07:11,480
It's the name of a variable, and Python uses the value whenever it sees the name, name in our case, in our code.

90
00:07:11,480 --> 00:07:15,460
We'll talk about variables more, in a video after the next one.

91
00:07:15,460 --> 00:07:19,740
First though, I want to finish discussing strings by looking at the escape character,

92
00:07:19,740 --> 00:07:23,420
and we'll be doing that in the next video.

