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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
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 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().'''
+
To change the '''string''' to '''lowercase''', we use the '''string method lower().'''
  
 
|-
 
|-

Latest revision as of 17:04, 13 August 2018

Visual Cue
Narration
Show Slide: Title Welcome to the spoken tutorial on Manipulating Strings.
Show Slide

Objectives


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
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite


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.

Show Slide


string slicing

First let us see about string slicing.


String slicing allows us to obtain substrings.


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

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3


Press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type, data = “python” Let us understand the string slicing with an example.


Type, data is equal to inside double quotes python

Type, print (data[0:3])


Highlight the output pyt

Type, print inside brackets data inside square brackets 0 colon 3


We get the sliced substring as pyt.

Note that, we are slicing the string from the index 0 to 3.


By doing so, the string elements from index 3 are not included.


<<PAUSE>>

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 as shown.

Type, s = "saturday"

Highlight ‘sat’

Type, s is equal to inside double quotes saturday


We need to check if the first three characters of the given string exists in the list week underscore name.

Type, s[0:3]


Highlight the output


Strings can be sliced into substrings.


To get the first three characters of the string s,

type, s inside square brackets 0 colon 3


As you can see, the character at last index ie 3 is not included in the sliced output.

Type, s[0:3] in week_name


Highlight True

We will check if the substring of s is present in the list week underscore name.


Type s inside square brackets 0 colon 3 in week underscore name


We get the output as True which indicates that the substring is present in the list.

Pause the video.


Try this exercise and then resume 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]


Highlight -1


Type, s inside square brackets 1 colon -1


As we already know, the last element of the string can be accessed using the index -1.


<<PAUSE>>

Type, s1 = "dad" Next let us learn to check if a given string is a palindrome or not.


A palindromic string is a string that is same as its reverse.


Type, s1 is equal to inside double quotes dad

Textbox s1[::-1] Now, we need to compare this string with its reverse.


For reversing s1, we stride the string from the first to last character.


It is done by keeping the start and stop values as empty and step as -1.


That is s1 inside square brackets colon colon -1

Type, s1 == s1[::-1]


Highlight the output

Now, we will check if the string s1 is a palindrome or not.


Type, s1 is double equal to s1 inside square brackets colon colon -1


The output True indicates that it is a palindrome.

Textbox "Dad" If the given string has capital D in dad instead of small d, the comparison would return False.


So, we have to convert the string to all lowercase or uppercase, before comparison.

Type, s2 = "Dad" Python provides the string methods lower and upper to achieve this.


Let's try it out.


Type, s2 is equal to inside double quotes capital D ad

Type, s2.upper() Type s2.upper open and close parentheses


We get all the characters in uppercase.

Type, s2


Highlight the output

Type s2


As you can see, s2 has not changed.


It is because, method upper returns a new string.


It doesn't change the original string.

Type, s2.lower() == s2.lower()[::-1]


Highlight the output

Let us compare the original and reversed string in lowercase.


Type as shown.


As expected the output is True.

Pause the video.


Try this exercise and then resume 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)


Highlight python False

Type as shown.


It returns False for python as it is not there in the list week underscore name.

Type, email = "info[at]fossee[dot]in"


Highlight at and dot

Next let us learn to use replace method.


Type, email is equal to inside double quotes info[at]fossee[dot]in


We often encounter email addresses which have @ symbol and periods replaced with text as shown.


Let us learn how to get back proper email addresses.

Type, email_at = email.replace("[at]", "@")

print (email_at)


Now, we will first replace the [at] with the @ symbol, using the replace method.


Type as shown.


You can see that the @ symbol is properly replaced now.

Pause the video.


Try this exercise and then resume 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.


Now the variable email underscore proper has the email in the proper form.


<<PAUSE>>

Type, email_list = ["info@fossee.in", "enquiries@fossee.in", "help@fossee.in"] Next, we will look at another interesting problem.


Let us say, we have a list of email addresses in the variable email underscore list as shown.


We will obtain one long string of email addresses, separated by commas or semicolons.

Type, email_str = ", ".join(email_list)


We will use the string method join for joining the list items into a single string.


Type as shown.


Comma followed by a space will give the string with the same formatting.

Type, print (email_str)


Highlight the output

Type, print inside parentheses email underscore str


You can see that all the email addresses joined into a single string.

Pause the video.


Try this exercise and then resume 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)


Highlight the output

Type as shown.


We see that the email addresses are joined by a semicolon followed by a space.


<<PAUSE>>

Show Slide

Summary


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Obtain substrings
  • Reverse strings by using the index numbers
  • Use the following methods
    • upper()
    • lower()
    • replace() and
    • join()
Show Slide

Self assessment questions

s = "this was a string"

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"
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.


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

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

http://spoken-tutorial.org

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.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst