Difference between revisions of "Python/C3/Manipulating-strings/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by 2 users not shown)
Line 10: Line 10:
 
| 00:04
 
| 00:04
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
 
+
Slice strings and get sub-strings out of them.
# Slice strings and get sub-strings out of them.
+
Reverse strings.
# Reverse strings.
+
Replace characters in strings.
# Replace characters in strings.
+
Convert strings to upper or lower case.
# Convert strings to upper or lower case.
+
Join a list of strings.
# Join a list of strings.
+
  
 
|-
 
|-
Line 122: Line 121:
  
 
|-
 
|-
|3:37
+
|03:37
 
|Now let us check if a particular substring is present in the variable week .  
 
|Now let us check if a particular substring is present in the variable week .  
  
Line 271: Line 270:
 
|-
 
|-
 
|08:03
 
|08:03
|<nowiki>Now, let us consider another problem.
+
|Now, let us consider another problem.
  
 
|-
 
|-
Line 343: Line 342:
 
|-
 
|-
 
|  10:42
 
|  10:42
| Now, if we wish to obtain one long string, separating each of the email id by a comma, we use the join operation on email_str is equal to in double quotes comma
+
| Now, if we wish to obtain one long string, separating each of the email id by a comma, we use the join operation on email_str is equal to in double quotes comma dot join then in brackets email underscore list
dot join then in brackets email underscore list
+
  
 
|-
 
|-
Line 396: Line 394:
 
|-
 
|-
 
|12:41
 
|12:41
|2. Use following functions - - <tt>upper()</tt> -- to obtain the upper case of a string - <tt>lower()</tt> -- to obtain the lower case of a string - <tt>replace()</tt> -- to replace a character by another one - <tt>join()</tt> -- to join a list of strings with an operator
+
|Use following functions - - <tt>upper()</tt> -- to obtain the upper case of a string - <tt>lower()</tt> -- to obtain the lower case of a string - <tt>replace()</tt> -- to replace a character by another one - <tt>join()</tt> -- to join a list of strings with an operator
  
 
|-
 
|-
Line 404: Line 402:
 
|-
 
|-
 
|13:01
 
|13:01
|1. Given a string <tt>s = "this is a string"</tt>, how will you change it to <tt>"this isn't a list"</tt> ?
+
|Given a string <tt>s = "this is a string"</tt>, how will you change it to <tt>"this isn't a list"</tt> ?
  
 
|-
 
|-
Line 416: Line 414:
 
|-
 
|-
 
|13:28
 
|13:28
|1. We will use the <tt>replace</tt> function to accomplish this.
+
|We will use the <tt>replace</tt> function to accomplish this.
  
 
|-
 
|-

Latest revision as of 12:32, 27 March 2017

Time Narration
00:00 Hi and Welcome to this tutorial on 'manipulating strings'.
00:04 At the end of this tutorial, you will be able to,

Slice strings and get sub-strings out of them. Reverse strings. Replace characters in strings. Convert strings to upper or lower case. Join a list of strings.

00:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "getting started with strings", "getting started with lists" and "basic datatypes and operators".
00:31 Let us invoke our ipython interpreter
00:34 So type ipython in the command.
00:39 Let us consider a simple problem, and learn how to slice strings and get sub-strings.
00:46 Let's say the variable week has the list of the names of the days of the week.
00:52 that is week is equal to within square brackets in double quotes sunday sun then comma mon for monday ,then tuesday so tue,then wednesday thursday, friday and Saturday.
01:15 Let us consider a simple problem.
01:18 So now given a string s , we should be able to check if the string is a valid name of a day of the week or not.
01:29 Let us define our string as saturday
01:32 So type in double quotes saturday
01:36 s is equal to saturday
01:41 s could be in any of the form
01:46 that is sat saturday, then s captial a t then capital s saturday then sat centre letter in capital then whole saturday in capital letters
02:05 For now, we shall be solving the problem only for the forms,small sat and small saturday.
02:16 We shall solve it for the other forms, at the end of the tutorial.
02:23 So, we need to check if the first three characters of the given string exists in the variable week .
02:31 As with any of the sequence data-types, strings can be sliced into sub-strings.
02:37 To get the first three characters of s, we say,
02:41 s in closing brackets, square brackets 0 colon 3 and hit enter.
02:48 Note that, we are slicing the string from the index 0 to index 3, 3 not included.
02:55 As we already know, the last element of the string can be accessed using s within brackets -1
03:02 Pause the video here, try out the following exercise and resume the video.
03:09 Obtain the sub-string excluding the first and last characters from the string s.
03:16 Switch to the terminal for solution.
03:18 Type s in square brackets 1 colon -1 and hit enter.
03:30 We get the substring of s, without the first and the last characters of s.
03:37 Now let us check if a particular substring is present in the variable week .
03:41 We shall check for 'sat'.
03:46 So type s in square brackets colon 3 and hit enter.
03:52 then s colon 3 in week
04:09 We get the result as true.
04:11 Let us now consider the problem of finding out, if a given string is palindromic or not.
04:18 First of all, a palindromic string is a string that remains same even when it has been reversed.
04:26 Let the string given be malayalam
04:30 So s1 is equal to malayalam
04:35 So malayalam should be in double quotes.
04:40 Now, we need to compare this string with it's reverse.
04:45 Again, we will use a technique common to all sequence data-types, that is, within square brackets colon colon -1
04:55 So, we obtain the reverse of s, by simply saying,s1 and within square brackets colon colon -1 and hit enter.
05:06 Now, to check if the string is s is palindromic, we say s1 is equal to is equal to s1 in square brackets colon colon -1
05:23 As, expected, we get True.
05:26 Now, if the string we are given is Malayalam instead of malayalam , the above comparison would return a False.
05:36 So, we will have to convert the string to a lower case or to all upper case, before comparing.
05:43 Python provides methods, s dot lower and s dot upper to achieve this.
05:48 Let's try it out.
05:51 So type s1 is equal to Malayalam in double quotes where m is capital.
06:03 then s1 dot upper then closing brackets.
06:10 then s1 will give u the output.
06:14 okay. So as you can see, s has not changed.
06:18 It is because, upper returns a new string.
06:20 It doesn't change the original string.
06:22 Similarly, type s1 dot lower closing brackets.
06:32 Then s1 dot lower closing brackets is equal to is equal to
06:40 s1 dot lower closing brackets within square bracket colon colon -1
06:51 So pause the video here, try out the following exercise and resume the video.
06:57 Check if s is a valid name of a day of the week.
07:02 Change the solution to this problem, to include forms like, SAT, SATURDAY, Saturday and Sat.
07:19 Switch to your terminal for solution
07:22 Type s in week. Then s dot lower closing bracket
07:33 then in square brackets colon 3
07:37 then in week
07:43 So, as you can see, now we can check for presence of s in week, in whichever format it is present -- capitalized, or all caps, full name or short form.
07:56 We just convert any input string to lower case and then check if it is present in the list week.
08:03 Now, let us consider another problem.
08:05 We often encounter e-mail id's which have '@' and periods replaced with text, something like info[at]fossee[dot]in.
08:26 We now wish to get back proper e-mail addresses.</nowiki>
08:30 Let's say the variable email has the email address.
08:34 So type email is equal to
08:38 within quotes double quotes info in square brackets at then fossee then again square bracket foss then dot then in.
08:48 Now, we first replace the [at] with the @, using the replace method of strings.
08:57 so type email is equal to email.replace
09:05 then in brackets double quotes @ at square brackets comma then again in square bracket
09:14 Now, type print email to get the output.
09:22 So pause the video here, try out the following exercise and resume the video.
09:30 Replace the [dot] with . in email
09:38 Switch to the terminal for solution
09:43 Type email is equal to email.replace within brackets in double quotes dot square brackets
10:02 then again in double quotes a dot.
10:07 Now, let us look at another interesting problem where we have a list of e-mail addresses and we wish to obtain one long string of e-mail addresses separated by commas or semi-colons.
10:23 So type email underscore list
10:30 is equal to within square brackets double quotes info at the rate fossee dot in, enquiries at the rate fossee dot in,then again comma in double quotes help at the rate fossee dot in
10:42 Now, if we wish to obtain one long string, separating each of the email id by a comma, we use the join operation on email_str is equal to in double quotes comma dot join then in brackets email underscore list
11:13 then hit enter.
11:15 then print email underscore str for output.
11:22 so type print email underscore str
11:28 So now, notice that the email ids are joined by a comma followed by a space.
11:34 So now pause the video here, try out the following exercise and resume the video.
11:41 For the email underscore str that we generated, change the separator to be a semicolon instead of a comma.
11:51 Then for that now switch to the terminal for solution.
11:56 Type email underscore str is equal to email underscore str dot replace then in brackets in double quotes comma then in another double quotes semicolon.
12:14 Then type print email underscore str.
12:26 We see that the email ids are joined by a semicolon followed by a space.
12:32 This brings us to the end of this tutorial.
12:35 In this tutorial, we have learnt to, Obtain sub-strings and reverse of strings by using the index numbers
12:41 Use following functions - - upper() -- to obtain the upper case of a string - lower() -- to obtain the lower case of a string - replace() -- to replace a character by another one - join() -- to join a list of strings with an operator
12:57 Here are some self assessment questions for you to solve
13:01 Given a string s = "this is a string", how will you change it to "this isn't a list" ?
13:09 Given the string "F.R.I.E.N.D.S" in s, obtain the string "friends" where the friends in s is all capital letters separated by dots.
13:26 And the answers,
13:28 We will use the replace function to accomplish this.
13:31 so type s = s dot replace within brackets and double quotes string,again brackets and double quotes list.
13:40 then s = s dot replace within brackets and double quotes is, again brackets and double quotes isn't
13:51 We notice that every 'is' in the statement has been replaced by isn't.
13:59 2. In order to change the string to lower case, we use the method lower()
14:05 So type s in square brackets colon colon 2 dot lower closing brackets.
14:13 So we hope you have enjoyed this tutorial and found it useful.
14:16 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha