Difference between revisions of "Python-3.4.3/C3/Manipulating-strings/English"
Nancyvarkey (Talk | contribs) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 21: | Line 21: | ||
* Convert a '''string''' to upper or lowercase and | * Convert a '''string''' to upper or lowercase and | ||
* Join '''list''' '''elements''' to form a '''string''' | * Join '''list''' '''elements''' to form a '''string''' | ||
− | |||
− | |||
|- | |- | ||
Line 33: | Line 31: | ||
* '''Python 3.4.3''' and | * '''Python 3.4.3''' and | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 359: | Line 355: | ||
− | Now the variable ''' | + | Now the variable '''email''''' underscore '''''proper''' has the '''email '''in the proper form. |
Line 369: | Line 365: | ||
− | Let us say, we have a list of '''email addresses''' in the variable ''' | + | Let us say, we have a list of '''email addresses''' in the variable '''email''''' underscore '''''list''' as shown. |
Line 392: | Line 388: | ||
Highlight the output | Highlight the output | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''print '''''inside parentheses''''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, '''print '''''inside parentheses''''' email''''' underscore '''''str''' |
Line 406: | Line 402: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Show Slide Assignment 4 | | style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Show Slide Assignment 4 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| From the ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| From the '''email''''' underscore '''''str''', change the separator to '''semicolon''' instead of '''comma'''. |
|- | |- | ||
Line 446: | Line 442: | ||
** '''replace() '''and | ** '''replace() '''and | ||
** '''join()''' | ** '''join()''' | ||
− | |||
− | |||
|- | |- | ||
Line 460: | Line 454: | ||
# Given a string '''s = "this was a string”''', how will you change it to "'''this wasn't a list'''"? | # Given a string '''s = "this was a string”''', how will you change it to "'''this wasn't a list'''"? | ||
# The string '''s''' is assigned as shown, change the string to "'''friends'''" | # The string '''s''' is assigned as shown, change the string to "'''friends'''" | ||
− | |||
− | |||
|- | |- | ||
Line 486: | Line 478: | ||
'''<nowiki>s[::2].lower()</nowiki>''' | '''<nowiki>s[::2].lower()</nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| 2. In order to remove dots and to get '''substring '''we '''stride '''the '''string s | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| 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().''' | ||
|- | |- |
Latest revision as of 17:04, 13 August 2018
|
|
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. |