Python/C3/Getting-started-with-strings/English-timed
From Script | Spoken-Tutorial
Timing | Narration |
---|---|
0:01 | Hello friends and Welcome to the tutorial on "Getting started with strings". |
0:06 | At the end of this tutorial, you will be able to,
|
0:25 | Open the terminal and invoke the ipython interpreter by typing ipython, so type ipython and hit enter |
0:35 | So, what are strings? |
0:38 | In Python anything within either single quotes or double quotes or triple single quotes or triple double quotes are strings. |
0:51 | So if you can type within single quotes This is a string, then in double quotes This is a string too. |
1:06 | Then in triple double quotes This is also a string |
1:26 | Note that it really doesn't matter how many characters are present in the string. |
1:32 | So we can even put a null string or an empty string. |
1:40 | Having more than one control character to define strings is handy when one of the control characters itself is part of the string. |
1:50 | For example you can type in double quotes Python's string manipulation functions are very useful |
2:16 | By having multiple control characters, we avoid the need for escaping characters hyphen hyphen in this case the apostrophe. |
2:28 | Let us now move on to the triple quoted strings. |
2:32 | Let us define multi-line strings without using any escaping. |
2:36 | Everything within the triple quotes is a single string no matter how many lines it extends |
2:44 | So we can type in the terminal, within triple double quotes Having more than one control character to define strings come as very handy when one of the control characters itself is part of the string. |
2:59 | So, we can assign this string to any variable |
3:04 | So lets type a = within single quotes Hello comma World exclamation and hit enter |
3:17 | Now 'a' is a string variable. |
3:21 | String is a collection of characters. |
3:23 | In addition string is an immutable collection which means that the string cannot be modified after it is created. |
3:30 | So all the operations that are applicable to any other immutable collection in Python, works on strings as well. |
3:38 | Hence we can add two strings |
3:41 | So lets type a = within single quotes Hello and hit enter, b = within single quotes World |
3:58 | c = a plus in single quotes comma plus b plus in single quotes exclamation and hit enter then
print c |
4:25 | So you will get an output hello comma world |
4:33 | We can add string variables as well as the strings themselves in all the same statement. |
4:38 | The same addition operation performs the concatenation of two strings. |
4:44 | Similarly we can multiply a string with an integer |
4:48 | So lets type a = in single quotes Hello |
4:58 | Type a into 5 |
5:03 | So we will get output as hello into 5 |
5:09 | It gives another string in which the original string 'Hello' is repeated 5 times. |
5:16 | Now, pause the video here, try out the following exercise and resume the video. |
5:22 | Obtain the string percentage percentage 20 hyphens percentage percentage without typing out all the twenty hyphens. |
5:32 | Let's now look at accessing individual elements of strings. |
5:37 | Since, strings are collections, we can access individual items in the string using the subscripts |
5:44 | So type s = within double quotes percentage percentage plus within double quotes hyphen multiply by 20 plus within double quotes percentage percentage and hit enter |
6:11 | Now to get an output print s |
6:20 | a in square bracket zero gives us the first character in the string. |
6:26 | The indexing starts from 0 for the first character and goes up to (n minus 1) for the last character, where 'n' is the total number of characters in a string. |
6:39 | We can access the strings from the end using negative indices |
6:44 | So type a in square bracket zero and hit enter |
6:50 | a in square bracket minus 1 gives us the last element of the string and a[-2] gives us second element from the end of the string.</nowiki>So type a in square bracket minus 1 and hit enter, then a in square bracket minus 2 and hit enter |
7:10 | Pause the video here, try out the following exercise and resume the video. |
7:14 | Given a string, s = within double quotes Hello World what is the output of |
7:22 | s[ minus 5]
|
7:25 | s[ minus 10] |
7:27 | s[minus 15] |
7:34 | Now, s of [-5] gives us 'W'.So in terminal type s of [-5] and hit enter. |
7:45 | s of [-10] gives us 'e' and |
7:51 | So we got an error as hyphen,so lets update the value of s with hello world. So type s = Hello World |
8:09 | Now s of -5 is W. So we got the output as W |
8:18 | So next type s of -10, it gives us e |
8:26 | And s of [-15] gives us an IndexError,as should be expected, since the string given to us is only 11 characters long. |
8:37 | Let us attempt to change one of the characters in a string |
8:42 | So type a = 'hello' and a of [0] = 'H' |
8:58 | As said earlier, strings are immutable. |
9:01 | We cannot manipulate a string. |
9:03 | Although there are some methods which let us manipulate strings, we will look at them in the advanced session on strings. |
9:10 | In addition to the methods that let us manipulate the strings we have methods like split which lets us break the string on the specified separator, the join method which lets us combine the list of strings into a single string based on the specified separator. |
9:30 | Let's revise quickly what we have learnt today. |
9:33 | In this tutorial we have learnt to, |
9:36 | 1. Define strings in different ways. |
9:39 | 2. Concatenate strings by performing addition. |
9:42 | 3.Repeat a string 'n' number of times by doing multiplication. |
9:47 | 4. Access individual elements of the string by using their subscripts. |
9:53 | 5. And finally use of the concept of immutability of strings.
|
9:58 | Here are some self assessment questions for you to solve |
10:02 | 1.Write code to assign s, the string ' is called the apostrophe |
10:11 | 2. Given strings s and t,s = "Hello" and t = "World" and an integer r, r = 2 |
10:21 | What is the output of s multiply r plus s multiply t? |
10:27 | 3.How will you change s='hello' to s='Hello'with h in capital |
10:37 | So, s of [0]= H |
10:40 | s of [0]=in single quotes h |
10:44 | Third option is strings are immutable,hence cannot be manipulated
|
10:49 | Let's look at the answers, |
10:52 | 1. The given string can be assigned in this manner |
10:55 | So type s = in double quotes` is called the apostrophe |
11:06 | 2. The operation s into r plus s into t will print each of the two words twice Hello Hello World World |
11:20 | Strings are immutable. |
11:22 | Therefore they cannot be manipulated. |
11:26 | Hope you have enjoyed this tutorial and found it useful. |
11:29 | Thank you! |