Python-3.4.3/C3/Getting-started-with-strings/English-timed
Tiime | Narration |
00:01 | Hello Friends. Welcome to the tutorial on "Getting Started with Strings". |
00:07 | At the end of this tutorial, you will learn to,
Define strings in different ways. Concatenate strings. Print a string repeatedly. Access individual elements of the string. |
00:24 | To record this tutorial, I am using
Ubuntu Linux 16.04 operating system Python 3.4.3 and IPython 5.1.0 |
00:39 | To practise this tutorial, you should know how to
run basic Python commands on the ipython console |
00:48 | If not, see the pre-requisite Python tutorials on this website. |
00:54 | What are strings?
In Python, any character within a single/ double/ triple quotes is a string. |
01:03 | For example:
Single, double, and triple quotes can be used to denote a string as shown here. |
01:13 | Strings in triple quotes can be written in multiple lines. |
01:19 | We can also define empty strings. String is a collection of characters which cannot be modified after it is created. |
01:30 | Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously. Type ipython3 and press Enter. |
01:42 | Let us initialise the pylab package.Type %pylab and press Enter. |
01:51 | Let us clear the terminal.
We can assign a string to a variable called a. |
01:58 | Type a is equal to inside any quotes Hello comma World exclamation mark and
Press Enter |
02:09 | Now let us learn string concatenation. Let us assign strings to variables. |
02:16 | Type
x is equal to inside quotes Hello Press Enter y is equal to inside quotes World Press Enter |
02:32 | Now, let us add the two strings.
z is equal to x plus inside quotes comma plus y plus inside quotes exclamation mark. Press Enter |
02:52 | print inside parentheses z
Press Enter Here x and y are string variables. |
03:04 | The addition operation performs the concatenation of two strings. |
03:09 | Here we can see the output of string concatenation. |
03:14 | Let us clear the terminal.
What if we multiply a string with an integer? Let us find out! |
03:23 | Recall x string.
Press Enter x multiplied by 5 Press Enter |
03:33 | The string Hello is repeated 5 times after multiplying. |
03:39 | Pause the video. Try this exercise and then resume the video.
Obtain the string as shown without typing out all the twenty hyphens. |
03:51 | Switch to the terminal for solution. Type, s is equal to inside quotes two percentages plus inside quotes hyphen multiplied by twenty plus inside quotes two percentages
Press Enter |
04:14 | print inside parentheses s Press Enter |
04:20 | Let's now look at accessing individual elements of strings. We can access individual elements in the string using the subscripts. |
04:30 | Let us clear the terminal.
Recall a. Press Enter. Type, a inside square brackets zero press Enter, gives us the first character of the string. |
04:47 | The indexing starts from 0 and goes up to (n-1).
where n is the total number of characters in a string. |
04:57 | We can access the strings from the end using negative indices. |
05:02 | a inside square brackets minus one gives us the last element of the string i.e. exclamation mark |
05:11 | a inside square brackets minus two gives us the second element from the end of the string i.e. d |
05:21 | Pause the video. Try this exercise and then resume the video. Given a string, s which is Hello World, what is the output of the following |
05:33 | Switch to the terminal. |
05:35 | Let us clear the terminal.
Type s is equal to inside quotes Hello World press Enter |
05:46 | s inside square brackets minus five gives W
Similarly we will get e for s[-10]. |
05:59 | s inside square brackets minus 15 gives us an IndexError. Since the string given to us is only 11 characters long. |
06:12 | s inside square brackets 15 again gives IndexError for the same reason. |
06:20 | Let us attempt to change one of the characters in a string.
Let us clear the terminal. Recall x Press Enter |
06:31 | Let us manipulate x.
x inside square brackets zero equal to inside quotes B press Enter |
06:44 | Why do we get an error?
Initially the value of x[0] is ‘H’. |
06:52 | Now we are trying to assign another value ‘B’ to x[0]. Recall, strings cannot be modified after it is created. |
07:02 | We can split and join the strings by using the functions : split() and join() |
07:08 | To join a string, the syntax is str dot join inside parentheses sequence |
07:17 | Switch to the terminal. |
07:19 | Let us clear the terminal |
07:22 | Type s is equal to inside quotes hyphen press Enter seq is equal to inside parentheses a comma b comma c press Enter |
07:41 | print s dot join' inside parentheses seq press Enter |
07:49 | We will get output as a hyphen b hyphen c It has joined a, b, c with hyphen. |
07:57 | To split a string, syntax is str dot split open and close parentheses |
08:05 | Switch to the terminal
Type as shown press Enter print str dot split open and close parentheses press Enter |
08:19 | We get the output like this. It has split the strings at spaces. |
08:25 | This brings us to the end of this tutorial. |
08:28 | In this tutorial, we have learnt to,
Define strings in different ways. Concatenate strings. Print a string repeatedly and Access individual elements of the string. |
08:42 | Here are some self assessment questions for you to solve |
08:46 | 1. Write code to assign the string ’ is called the apostrophe, to variable s
2. How will you change s is equal to hello to s is equal to capital H Hello. |
09:01 | 3. The variables s and t are strings and r is integer. |
09:07 | They are assigned with the values as shown below, What is the output of s multiplied by r plus t multiplied by r? |
09:17 | And the answers,
The given string can be assigned in this manner Strings cannot be manipulated after it is assigned a value |
09:29 | The operation s multiplied by r plus t multiplied by r will print each of the two words twice as HelloHelloWorldWorld |
09:41 | Please post your timed queries in this forum. |
09:45 | Please post your general queries on Python in this forum. |
09:50 | FOSSEE team coordinates the TBC project. |
09:54 | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website. |
10:04 | This is Trupti Kini from IIT Bombay signing off. Thank you. |