1 00:00:00,000 --> 00:00:03,510 In Python, a string is a sequence of characters. 2 00:00:03,510 --> 00:00:06,380 A string is contained within two quotes. 3 00:00:06,380 --> 00:00:08,750 You could also use single quotes. 4 00:00:08,750 --> 00:00:11,580 A string can be spaces or digits. 5 00:00:11,580 --> 00:00:14,430 A string can also be special characters. 6 00:00:14,430 --> 00:00:17,740 We can bind or assign a string to another variable. 7 00:00:17,740 --> 00:00:21,060 It is helpful to think of a string as an ordered sequence. 8 00:00:21,060 --> 00:00:23,960 Each element in the sequence can be accessed using 9 00:00:23,960 --> 00:00:26,990 an index represented by the array of numbers. 10 00:00:26,990 --> 00:00:29,820 The first index can be accessed as follows: 11 00:00:29,820 --> 00:00:31,950 We can access index six. 12 00:00:31,950 --> 00:00:35,120 Moreover, we can access the 13th index. 13 00:00:35,120 --> 00:00:38,280 We can also use negative indexing with strings. 14 00:00:38,280 --> 00:00:41,400 The last element is given by the index negative one. 15 00:00:41,400 --> 00:00:45,990 The first element can be obtained by index negative 15 and so on. 16 00:00:45,990 --> 00:00:48,590 We can bind a string to another variable. 17 00:00:48,590 --> 00:00:51,920 It is helpful to think of string as a list or tuple. 18 00:00:51,920 --> 00:00:56,290 We can treat the string as a sequence and perform sequence operations. 19 00:00:56,290 --> 00:00:59,740 We can also input a stride value as follows: 20 00:00:59,740 --> 00:01:03,100 The two indicates we'd select every second variable. 21 00:01:03,100 --> 00:01:05,430 We can also incorporate slicing. 22 00:01:05,430 --> 00:01:09,580 In this case, we return every second value up to index four. 23 00:01:09,580 --> 00:01:13,470 We can use the len command to obtain the length of the string. 24 00:01:13,470 --> 00:01:17,140 As there are 15 elements, the result is 15. 25 00:01:17,140 --> 00:01:20,240 We can concatenate or combine strings. 26 00:01:20,240 --> 00:01:22,390 We use the addition symbols. 27 00:01:22,390 --> 00:01:25,680 The result is a new string that is a combination of both. 28 00:01:25,680 --> 00:01:28,260 We can replicate values of a string. 29 00:01:28,260 --> 00:01:32,380 We simply multiply the string by the number of times we would like to replicate it- 30 00:01:32,380 --> 00:01:33,820 in this case, three. 31 00:01:33,820 --> 00:01:35,550 The result is a new string. 32 00:01:35,550 --> 00:01:39,370 The new string consists of three copies of the original string. 33 00:01:39,370 --> 00:01:44,490 This means you cannot change the value of the string, but you can create a new string. 34 00:01:44,490 --> 00:01:47,980 For example, you can create a new string by setting it to 35 00:01:47,980 --> 00:01:51,690 the original variable and concatenate it with a new string. 36 00:01:51,690 --> 00:01:54,090 The result is a new string that changes from 37 00:01:54,090 --> 00:01:57,810 Michael Jackson to Michael Jackson is the best. 38 00:01:57,810 --> 00:01:59,490 Strings are immutable. 39 00:01:59,490 --> 00:02:02,970 Back slashes represent the beginning of escape sequences. 40 00:02:02,970 --> 00:02:06,710 Escape sequences represent strings that may be difficult to input. 41 00:02:06,710 --> 00:02:10,630 For example, backslashes "n" represent a new line. 42 00:02:10,630 --> 00:02:15,510 The output is given by a new line after the backslashes "n" is encountered. 43 00:02:15,510 --> 00:02:19,190 Similarly, backslash "t" represents a tab. 44 00:02:19,190 --> 00:02:23,450 The output is given by a tab where the backslash, "t" is. 45 00:02:23,450 --> 00:02:26,250 If you want to place a backslash in your string, 46 00:02:26,250 --> 00:02:27,960 use a double backslash. 47 00:02:27,960 --> 00:02:31,530 The result is a backslash after the escape sequence. 48 00:02:31,530 --> 00:02:34,980 We can also place an "r" in front of the string. 49 00:02:34,980 --> 00:02:37,960 Now, let's take a look at string methods. 50 00:02:37,960 --> 00:02:41,160 Strings are sequences and as such, 51 00:02:41,160 --> 00:02:44,380 have apply methods that work on lists and tuples. 52 00:02:44,380 --> 00:02:48,840 Strings also have a second set of methods that just work on strings. 53 00:02:48,840 --> 00:02:51,860 When we apply a method to the string A, 54 00:02:51,860 --> 00:02:55,230 we get a new string B that is different from A. 55 00:02:55,230 --> 00:02:57,030 Let's do some examples. 56 00:02:57,030 --> 00:02:59,050 Let's try with the method "Upper". 57 00:02:59,050 --> 00:03:03,450 This method converts lowercase characters to uppercase characters. 58 00:03:03,450 --> 00:03:07,530 In this example, we set the variable A to the following value. 59 00:03:07,530 --> 00:03:11,320 We apply the method "Upper", and set it equal to B. 60 00:03:11,320 --> 00:03:16,040 The value for B is similar to A, but all the characters are uppercase. 61 00:03:16,040 --> 00:03:19,470 The method replaces a segment of the string- i.e. 62 00:03:19,470 --> 00:03:21,590 a substring with a new string. 63 00:03:21,590 --> 00:03:24,960 We input the part of the string we would like to change. 64 00:03:24,960 --> 00:03:29,010 The second argument is what we would like to exchange the segment with. 65 00:03:29,010 --> 00:03:32,770 The result is a new string with a segment changed. 66 00:03:32,770 --> 00:03:36,340 The method find, find substrings. 67 00:03:36,340 --> 00:03:39,680 The argument is the substring you would like to find. 68 00:03:39,680 --> 00:03:43,350 The output is the first index of the sequence. 69 00:03:43,350 --> 00:03:46,210 We can find the substring Jack. 70 00:03:46,210 --> 00:03:48,720 If the substring is not in the string, 71 00:03:48,720 --> 00:03:50,360 the output is negative one. 72 00:03:50,360 --> 00:03:53,000 Check the labs for more examples. 73 00:03:53,000 --> 00:03:58,000 (Music)