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