Python-3.4.3/C3/Manipulating-strings/English
|
|
Show Slide: Title | Welcome to the spoken tutorial on Manipulating Strings. |
Show Slide
Objectives
|
In this tutorial, we will learn to
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite
|
To practise this tutorial, you should know how to use
If not, see the relevant Python tutorials on this website. |
Show Slide
|
First let us see about string slicing.
string_name inside square brackets start colon stop or string_name inside square brackets start colon stop colon step |
Open the terminal | Let us start ipython.
|
Type ipython3
|
Type ipython3 and press Enter.
|
Type, data = “python” | Let us understand the string slicing with an example.
|
Type, print (data[0:3])
|
Type, print inside brackets data inside square brackets 0 colon 3
Note that, we are slicing the string from the index 0 to 3.
|
Type, week_name = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] | Let us create a list, week underscore name which has names of the days of the week.
|
Type, s = "saturday"
Highlight ‘sat’ |
Type, s is equal to inside double quotes saturday
|
Type, s[0:3]
|
Strings can be sliced into substrings.
type, s inside square brackets 0 colon 3
|
Type, s[0:3] in week_name
|
We will check if the substring of s is present in the list week underscore name.
|
Pause the video.
| |
Show Slide
Assignment 1 |
Obtain the substring excluding the first and last characters from the string s. |
Switch to terminal | Switch back to terminal for the solution. |
s[1:-1]
|
Type, s inside square brackets 1 colon -1
|
Type, s1 = "dad" | Next let us learn to check if a given string is a palindrome or not.
|
Textbox s1[::-1] | Now, we need to compare this string with its reverse.
|
Type, s1 == s1[::-1]
|
Now, we will check if the string s1 is a palindrome or not.
|
Textbox "Dad" | If the given string has capital D in dad instead of small d, the comparison would return False.
|
Type, s2 = "Dad" | Python provides the string methods lower and upper to achieve this.
|
Type, s2.upper() | Type s2.upper open and close parentheses
|
Type, s2
|
Type s2
|
Type, s2.lower() == s2.lower()[::-1]
|
Let us compare the original and reversed string in lowercase.
|
Pause the video.
| |
Show Slide Assignment 2
['SATURDAY', 'python', 'Sunday'] |
Check if each element in the following list is present in the list week underscore name. |
Switch to terminal | Switch back to terminal for the solution. |
Type, for day in ['SATURDAY', 'python', 'Sunday']:print (day, day.lower()[:3] in week_name)
|
Type as shown.
|
Type, email = "info[at]fossee[dot]in"
|
Next let us learn to use replace method.
|
Type, email_at = email.replace("[at]", "@")
print (email_at)
|
Now, we will first replace the [at] with the @ symbol, using the replace method.
|
Pause the video.
| |
Show Slide Assignment 3
info@fossee[dot]in |
Replace the [dot] with '.' symbol in the given email. |
Switch to terminal | Switch back to terminal for the solution. |
Type, email_proper = email_at.replace("[dot]", ".")
print (email_proper) |
Type as shown.
|
Type, email_list = ["info@fossee.in", "enquiries@fossee.in", "help@fossee.in"] | Next, we will look at another interesting problem.
|
Type, email_str = ", ".join(email_list)
|
We will use the string method join for joining the list items into a single string.
|
Type, print (email_str)
|
Type, print inside parentheses email underscore str
|
Pause the video.
| |
Show Slide Assignment 4 | From the email underscore str, change the separator to semicolon instead of comma. |
Switch to terminal | Switch back to terminal for the solution. |
Type, email_str = email_str.replace(",", ";")
print (email_str)
|
Type as shown.
|
Show Slide
Summary
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Self assessment questions s = "this was a string" |
Here are some self assessment questions for you to solve
|
Show Slide
Solutions of self assessment questions s = s.replace("string", "list") s = s.replace("was", "wasn't") print(s) |
And the answers,
1. We will use the replace method to accomplish this.
|
Show Slide
Solution of self assessment questions s[::2].lower() |
2. In order to remove dots and to get substring we stride the string s.
To change the string to lowercase, we use the string method lower(). |
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Slide Textbook Companion | FOSSEE team coordinates the TBC project. (16.45) |
Show Slide Acknowledgment | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Priya from IIT Bombay signing off. Thanks for joining. |