Python-3.4.3/C3/Manipulating-strings/English-timed

From Script | Spoken-Tutorial
Revision as of 18:11, 4 June 2019 by Pratik kamble (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Manipulating Strings.
00:06 In this tutorial, we will learn to

Slice a string and get substrings out of them

Reverse a string

Replace characters in a string

Convert a string to upper or lowercase and

Join list elements to form a string

00:27 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3 and

IPython 5.1.0

00:43 To practise this tutorial, you should know how to use

basic datatypes, operators, strings and lists

If not, see the relevant Python tutorials on this website.

00:59 First let us see about string slicing.
01:03 String slicing allows us to obtain substrings.
01:08 The syntax for the string slicing is:

string_name inside square brackets start colon stop or

string_name inside square brackets start colon stop colon step

01:24 Let us start ipython. Open the terminal.
01:29 Type ipython3 and press Enter. From here onwards, remember to press the Enter key after typing every command on the terminal.
01:40 Let us understand the string slicing with an example. Type, data is equal to inside double quotes python
01:51 Type, print inside brackets data inside square brackets 0 colon 3
01:59 We get the sliced substring as pyt.
02:03 Note that, we are slicing the string from the index 0 to 3.
02:09 By doing so, the string elements from index 3 are not included.
02:15 Let us create a list, week underscore name which has names of the days of the week.
02:21 Type as shown. Type, s is equal to inside double quotes saturday
02:31 We need to check if the first three characters of the given string exists in the list week underscore name.
02:39 Strings can be sliced into substrings.
02:43 To get the first three characters of the string s, type, s inside square brackets 0 colon 3
02:52 As you can see, the character at last index ie 3 is not included in the sliced output.
03:00 We will check if the substring of s is present in the list week underscore name.
03:06 Type s inside square brackets 0 colon 3 in week underscore name
03:14 We get the output as True which indicates that the substring is present in the list.
03:20 Pause the video. Try this exercise and then resume the video.
03:26 Obtain the substring excluding the first and last characters from the string s.
03:32 Switch back to terminal for the solution.
03:36 Type, s inside square brackets 1 colon -1
03:42 As we already know, the last element of the string can be accessed using the index -1.


03:49 Next let us learn to check if a given string is a palindrome or not.
03:55 A palindromic string is a string that is same as its reverse.
04:01 Type, s1 is equal to inside double quotes dad
04:07 Now, we need to compare this string with its reverse.
04:11 For reversing s1, we stride the string from the first to last character.
04:17 It is done by keeping the start and stop values as empty and step as -1.
04:24 That is s1 inside square brackets colon colon -1
04:30 Now, we will check if the string s1 is a palindrome or not.
04:35 Type, s1 is double equal to s1 inside square brackets colon colon -1
04:44 The output True indicates that it is a palindrome.
04:49 If the given string has capital D in dad instead of small d, the comparison would return False.
04:57 So, we have to convert the string to all lowercase or uppercase, before comparison.
05:04 Python provides the string methods lower and upper to achieve this.
05:10 Let's try it out. Type, s2 is equal to inside double quotes capital D ad
05:19 Type s2.upper open and close parentheses We get all the characters in uppercase.
05:29 Type s2 As you can see, s2 has not changed.
05:36 It is because, method upper returns a new string. It doesn't change the original string.
05:43 Let us compare the original and reversed string in lowercase.

Type as shown. As expected the output is True.

05:56 Pause the video. Try this exercise and then resume the video.
06:02 Check if each element in the following list is present in the list week underscore name.
06:09 Switch back to terminal for the solution.
06:13 Type as shown. It returns False for python as it is not there in the list week underscore name.
06:24 Next let us learn to use replace method.
06:28 Type, email is equal to inside double quotes info[at]fossee[dot]in
06:36 We often encounter email addresses which have @ symbol and periods replaced with text as shown.
06:44 Let us learn how to get back proper email addresses.
06:49 Now, we will first replace the [at] with the @ symbol, using the replace method.
06:55 Type as shown. You can see that the @ symbol is properly replaced now.
07:05 Pause the video. Try this exercise and then resume the video.
07:11 Replace the [dot] with '.' symbol in the given email.
07:16 Switch back to terminal for the solution.
07:20 Type as shown.
07:24 Now the variable email underscore proper has the email in the proper form.
07:30 Next, we will look at another interesting problem.
07:34 Let us say, we have a list of email addresses in the variable email underscore list as shown.
07:41 We will obtain one long string of email addresses, separated by commas or semicolons.
07:48 We will use the string method join for joining the list items into a single string.
07:55 Type as shown. Comma followed by a space will give the string with the same formatting.
08:04 Type, print inside parentheses email underscore str You can see that all the email addresses joined into a single string.
08:16 Pause the video. Try this exercise and then resume the video.
08:22 From the email underscore str, change the separator to semicolon instead of comma.
08:29 Switch back to terminal for the solution.
08:33 Type as shown. We see that the email addresses are joined by a semicolon followed by a space.
08:43 This brings us to the end of this tutorial. Let us summarize.
08:49 In this tutorial, we have learnt to,

Obtain substrings

Reverse strings by using the index numbers

08:58 Use the following methods

upper()

lower()

replace() and

join()

09:05 Here are some self assessment questions for you to solve

1. Given a string s = "this was a string”, how will you change it to "this wasn't a list"?

2. The string s is assigned as shown, change the string to "friends"

09:25 And the answers,

1. We will use the replace method to accomplish this.

Here string and was in the statement will be replaced by list and wasn't respectively.

09:39 2. In order to remove dots and to get substring we stride the string s.
09:45 To change the string to lowercase, we use the string method lower().
09:51 Please post your timed queries in this forum.
09:55 Please post your general queries on Python in this forum.
10:00 FOSSEE team coordinates the TBC project. (16.45)
10:04 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
10:14 This is Priya from IIT Bombay signing off. Thanks for joining.

Contributors and Content Editors

Pratik kamble