Difference between revisions of "Python-3.4.3/C2/Getting-started-with-strings/English"
(Created page with "'''Python/C2/Getting Started with Strings/English'''<div style="margin-left:0cm;margin-right:0cm;"><span style="background-color:#ffffff;color:#000000;">'''Title of script'''<...") |
|||
Line 1: | Line 1: | ||
− | '''Python/C2/Getting Started with Strings/English''' | + | '''Python/C2/Getting Started with Strings/English''''''Title of script''': '''Getting Started with Strings''' |
− | + | '''Author: Trupti Kini''' | |
− | + | '''Keywords: Python, Ipython''' | |
+ | {| style="border-spacing:0;" | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center> | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <center>'''Narration'''</center> | ||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
− | + | containing title, name of the production team along with the logo of MHRD | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Hello Friends. Welcome to the tutorial on "'''Getting Started with Strings'''". |
− | | | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide |
− | | | + | |
− | | | + | |
− | + | Objectives | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| At the end of this tutorial, you will learn to, |
− | # | + | # Define strings in different ways. |
− | # | + | # Concatenate strings. |
− | # | + | # Print a string repeatedly. |
− | # | + | # Access individual elements of the string. |
− | |||
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |- |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show slide |
− | + | System Specifications | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To record this tutorial, I am using |
− | + | ||
− | + | ||
+ | * '''Ubuntu Linux 16.04''' operating system | ||
+ | * '''Python 3.4.3''' | ||
+ | * '''IPython 5.1.0''' | ||
− | |||
− | |||
− | |||
− | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
+ | Pre-requisites | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to | ||
− | + | * run basic '''Python '''commands on the '''ipython console''' | |
− | + | If not, see the pre-requisite '''Python '''tutorials on this website. | |
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | | | + | |- |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
What are Strings? | What are Strings? | ||
− | + | In '''Python,''' any character within a '''single/ double/ triple quotes '''is''' '''a''' string.''' | |
Line 68: | Line 68: | ||
‘’’This can be a large string’’’ | ‘’’This can be a large string’’’ | ||
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| What are '''strings'''? |
− | + | In '''Python,''' any character within a '''single/ double/ triple quotes '''is''' '''a''' string.''' | |
− | + | For example: | |
− | + | Single, double, and triple '''quotes''' can be used to denote a '''string '''as shown here. | |
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
What are Strings? | What are Strings? | ||
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''Strings''' in triple '''quotes''' can be written in multiple '''lines'''. |
− | + | We can also define empty '''strings'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''String''' is a collection of characters which cannot be modified after it is created. | '''String''' is a collection of characters which cannot be modified after it is created. | ||
+ | |||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | [Terminal] | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Terminal]</nowiki> |
'''ipython3''' | '''ipython3''' | ||
Line 102: | Line 99: | ||
− | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;" | Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously. |
− | + | Type '''ipython3''' and press '''Enter'''. | |
− | |- style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;" | + | |
− | + | |- | |
+ | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| <nowiki>[IPython console]</nowiki> | ||
'''%pylab '''and press '''Enter.''' | '''%pylab '''and press '''Enter.''' | ||
− | || Let us initialise the '''pylab''' package. | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.014cm;padding-right:0.191cm;"| Let us initialise the '''pylab''' package. |
Type '''%pylab '''and press''' Enter.''' | Type '''%pylab '''and press''' Enter.''' | ||
− | |||
− | |||
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> |
+ | |||
+ | '''a = 'Hello, World!' ''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us clear the terminal. | ||
+ | |||
+ | We can assign a '''string''' to a '''variable '''called '''a.''' | ||
− | + | Type '''a '''''is equal to inside any quotes'' '''Hello''' ''comma'' '''World''' '''exclamation mark '''and | |
Press '''Enter''' | Press '''Enter''' | ||
− | |||
− | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
− | |||
− | + | ||
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now let us learn string concatenation. |
− | + | ||
+ | Let us assign strings to variables. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''x = 'Hello' ''' | ||
'''y = 'World'''' | '''y = 'World'''' | ||
− | |||
− | + | ||
− | || Type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Type |
'''x '''''is equal to inside quotes''''' Hello''' | '''x '''''is equal to inside quotes''''' Hello''' | ||
Line 149: | Line 151: | ||
Press '''Enter''' | Press '''Enter''' | ||
− | Now, let us add the two strings. | + | |- |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''z = x + ', ' + y + '!'''' | ||
+ | |||
+ | '''print(z)''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Now, let us add the two strings. | ||
Line 164: | Line 170: | ||
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight + |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The '''addition''' '''operation''' performs the '''concatenation''' of two '''strings'''. |
− | | | + | |
− | + | |- | |
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight the output |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here we can see the output of string concatenation. | |
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
Press Up arrow till we get '''x''' | Press Up arrow till we get '''x''' | ||
− | '''x | + | '''x * 5''' |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us clear the terminal |
+ | |||
+ | What if we multiply a string with an integer? | ||
Let us find out! | Let us find out! | ||
− | |||
− | |||
Recall''' x''' string. | Recall''' x''' string. | ||
Line 190: | Line 199: | ||
Press '''Enter''' | Press '''Enter''' | ||
− | |||
− | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Ipython Terminal | ||
− | |||
− | |||
− | |||
− | |||
+ | Highlight the output | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The '''string''' '''Hello''' is repeated 5 times after '''multiplying'''. | ||
− | + | |- | |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | |
− | + | Exercise 1 | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | ||
− | + | # Obtain the '''string''' as shown without '''typing''' out all the twenty '''hyphens'''. | |
− | + | ||
− | |||
− | + | |- | |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | |
− | + | '''s = "%% " + "-"*20 + "%%"''' | |
− | + | '''print(s)''' | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal for solution. | ||
+ | |||
+ | |||
+ | Type, | ||
+ | |||
+ | '''s '''''is equal to inside quotes''''' two percentages '''''plus inside quotes''''' hyphen '''''multiplied by''''' twenty''''' plus''''' '''''inside quotes''''' two percentages''' | ||
Press '''Enter''' | Press '''Enter''' | ||
Line 226: | Line 238: | ||
Press '''Enter''' | Press '''Enter''' | ||
− | |||
− | |||
− | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let's now look at '''accessing''' individual '''elements''' of '''strings'''. | ||
− | |||
− | |||
− | |||
− | + | We can access individual elements in the '''string''' using the '''subscripts.''' | |
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
Press Up arrow till we get '''a''' | Press Up arrow till we get '''a''' | ||
− | + | '''<nowiki>a[0] </nowiki>''' | |
− | || Recall '''a.''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us clear the terminal. |
+ | |||
+ | Recall '''a.''' | ||
+ | |||
+ | Press Enter. | ||
Type, | Type, | ||
− | + | '''a '''''inside square brackets zero ''press enter, gives us the first '''character''' of the '''string'''. | |
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| The '''indexing''' starts from 0 and goes up to '''(n-1)'''. | ||
− | |||
+ | where 'n' is the total number of '''characters''' in a '''string'''. | ||
− | |||
+ | We can '''access''' the '''strings''' from the end using '''negative''' '''indices.''' | ||
− | |||
− | |||
− | |||
− | |||
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> |
− | + | '''<nowiki>a[-1] </nowiki>''' | |
− | + | ||
− | + | ||
− | < | + | '''<nowiki>a[-2]</nowiki>''' |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''a '''''inside square brackets '''''minus one'' '''''gives us the last '''element''' of the '''string''' i.e. '''exclamation mark''' | ||
+ | |||
+ | |||
+ | '''a '''''inside square brackets '''''minus two''' gives us the second '''element''' from the end of the '''string''' i.e. '''d''' | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
Exercise 2 | Exercise 2 | ||
Line 279: | Line 299: | ||
what is the output of | what is the output of | ||
− | '''s[-5]''' | + | '''<nowiki>s[-5]</nowiki>''' |
− | '''s[-10]''' | + | '''<nowiki>s[-10]</nowiki>''' |
− | '''s[-15] ''' | + | '''<nowiki>s[-15] </nowiki>''' |
− | '''s[15]''' | + | '''<nowiki>s[15]</nowiki>''' |
− | |||
− | |||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | ||
− | |||
− | + | Given a string, '''s''' which is '''Hello World''', what is the output of the following | |
− | + | '''<nowiki>s[-5]</nowiki>''' | |
− | + | '''<nowiki>s[-10]</nowiki>''' | |
− | + | '''<nowiki>s[-15] </nowiki>''' | |
− | + | ||
− | + | ||
− | + | '''<nowiki>s[15]</nowiki>''' | |
− | + | |- | |
− | || Switch to the terminal. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> |
+ | |||
+ | '''s = "Hello World”''' | ||
+ | |||
+ | '''<nowiki>s[-5]</nowiki>''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal. | ||
+ | |||
+ | Let us clear the terminal. | ||
Type | Type | ||
Line 313: | Line 336: | ||
'''s '''''is equal to inside quotes''''' Hello World '''press '''Enter''' | '''s '''''is equal to inside quotes''''' Hello World '''press '''Enter''' | ||
− | + | '''s '''''inside square brackets '''''minus five''' gives '''W ''' | |
− | Similarly we will get '''e '''for s[-10]. | + | Similarly we will get '''e '''<nowiki>for s[-10].</nowiki> |
− | + | ||
− | + | ||
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> |
− | | | + | |
− | + | ||
− | + | '''<nowiki>s[-15]</nowiki>''' | |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''s '''''inside square brackets '''''minus 15''' gives us an '''IndexError. '''Since the string given to us is only 11 '''characters''' long. | |
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
+ | |||
+ | '''<nowiki>s[15]</nowiki>''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''s '''''inside square brackets '''''15 '''again gives '''IndexError '''for the same reason. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
Press Up arrow till we get '''x''' | Press Up arrow till we get '''x''' | ||
− | '''x< | + | '''x<nowiki>[0] = '</nowiki>B'''' |
− | |||
− | |||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Let us attempt to change one of the '''characters''' in a '''string'''. | |
+ | |||
+ | Let us clear the terminal. | ||
Recall '''x''' | Recall '''x''' | ||
+ | |||
+ | Press Enter | ||
Let us manipulate '''x.''' | Let us manipulate '''x.''' | ||
Line 345: | Line 373: | ||
'''x '''''inside square brackets '''''zero '''''equal to inside quotes '''''B '''press '''Enter''' | '''x '''''inside square brackets '''''zero '''''equal to inside quotes '''''B '''press '''Enter''' | ||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| '''Highlight the error''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Why do we get an error? | ||
− | + | Initially the value of <nowiki>x[0] is ‘H’. </nowiki> | |
− | + | <nowiki>Now we are trying to assign another value ‘B’ to x[0].</nowiki> | |
Recall, strings cannot be modified after it is created. | Recall, strings cannot be modified after it is created. | ||
− | |||
− | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show slide | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We can split and join the strings by using the functions : | ||
+ | '''split()''' and''' join()''' | ||
− | |||
− | |||
− | |||
− | |||
− | + | |- | |
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show slide |
− | + | ||
Syntax for''' join()''' | Syntax for''' join()''' | ||
'''str.join(sequence)''' | '''str.join(sequence)''' | ||
− | || | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To join a string, the syntax is |
− | + | ||
− | To join a string | + | |
'''str '''''dot join inside parentheses'' '''sequence''' | '''str '''''dot join inside parentheses'' '''sequence''' | ||
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
s = “-”seq = ("a", "b", "c"); print (s.join( seq )) | s = “-”seq = ("a", "b", "c"); print (s.join( seq )) | ||
Line 385: | Line 412: | ||
− | || Switch to the terminal. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal. |
+ | |||
+ | Let us clear the terminal | ||
Type | Type | ||
− | s ''is equal to inside quotes '''''hyphen '''press '''Enter'''seq ''is equal to inside parentheses '''''a | + | s ''is equal to inside quotes '''''hyphen '''press '''Enter'''seq ''is equal to inside parentheses '''''a '''''comma '''''b '''''comma '''''c '''press '''Enter''' |
print s ''dot '''''join''''' inside parentheses'' '''seq '''press '''Enter''' | print s ''dot '''''join''''' inside parentheses'' '''seq '''press '''Enter''' | ||
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | + | |- | |
− | || We will get output as '''a | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight output |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We will get output as '''a '''''hyphen '''''b '''''hyphen '''''c''' | ||
It has joined a, b, c with hyphen. | It has joined a, b, c with hyphen. | ||
− | |||
− | |||
− | Show slide syntax for '''split()''' | + | |- |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show slide syntax for '''split()''' | ||
str.split() | str.split() | ||
− | || To split a string | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| To split a string, syntax is |
− | str ''dot ''split ''open and close parentheses '' | + | '''str''' ''dot '''''split '''''open and close parentheses '' |
− | |- style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;" | + | |
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| <nowiki>[Ipython Terminal]</nowiki> | ||
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";print (str.split( )) | str = "Line1-abcdef \nLine2-abc \nLine4-abcd";print (str.split( )) | ||
Line 414: | Line 444: | ||
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Switch to the terminal |
− | + | ||
− | | | + | |
− | + | ||
− | + | str ''is equal to ''type as shown press '''Enter'''print '''str''' ''dot '''''split''' ''open and close parentheses''''' '''press '''Enter''' | |
− | + | ||
− | + | ||
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Highlight output | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| We get the output like this. | ||
+ | It has split the string at spaces. | ||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
− | + | Summary slide | |
− | |||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This brings us to the end of this tutorial. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | In this tutorial, we have learnt to,''' ''' | ||
+ | * Define strings in different ways. | ||
+ | * Concatenate strings. | ||
+ | * Print a string repeatedly. | ||
+ | * Access individual elements of the string. | ||
− | |||
− | |||
− | + | |- | |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | |
− | + | ||
+ | Evaluation | ||
+ | # Write code to assign the string ' is called the apostrophe , to a variable s | ||
+ | # How will you change s='hello' to s='Hello'. | ||
+ | # The variables s and t are string and r is integer. | ||
s=”Hello” | s=”Hello” | ||
Line 460: | Line 488: | ||
− | What is the output of | + | What is the output of s * r + t * r? |
− | + | ||
− | + | ||
− | |||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | |
− | + | ||
− | + | ||
+ | # Write code to assign the string ''inside quotes ''is called the apostrophe , to variable s | ||
+ | # How will you change s ''is equal to '''hello' to s ''is equal to capital h '''Hello'. | ||
+ | # The variables s and t are strings and r is integer. | ||
They are assigned with the values as shown below: | They are assigned with the values as shown below: | ||
Line 482: | Line 508: | ||
− | What is the output of | + | What is the output of s ''multiplied by'' r'' plus'' t ''multiplied by'' r? |
− | |||
− | |||
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
+ | Solutions | ||
− | |||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| And the answers,''' ''' | ||
+ | # The given string can be assigned in this manner | ||
+ | # Strings cannot be manipulated after it is assigned a value. | ||
− | + | # The operation s ''multiplied by'' r'' plus'' t ''multiplied by'' r will print each of the two words twice as | |
− | + | '''HelloHelloWorldWorld ''' | |
− | |||
− | |||
− | + | |- | |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
+ | Forum | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Please post your timed queries in this forum. | ||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide | ||
− | | | + | Fossee Forum |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Please post your general queries on Python in this forum. |
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide |
− | | | + | |
− | | | + | |
− | + | Textbook Companion | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| FOSSEE team coordinates the TBC project. |
− | + | ||
− | | | + | |
− | + | |- | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide |
− | | | + | |
− | | | + | |
− | + | Acknowledgment | |
− | + | http://spoken-tutorial.org | |
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. |
− | + | For more details, visit this website. | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| Show Slide |
+ | Thank You | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.088cm;padding-right:0.191cm;"| This is _________ from IIT Bombay signing off. | ||
− | + | Thank you. | |
+ | |||
+ | |} |
Revision as of 12:13, 12 December 2017
'Python/C2/Getting Started with Strings/English'Title of script: Getting Started with Strings
Author: Trupti Kini
Keywords: Python, Ipython
|
|
Show Slide
containing title, name of the production team along with the logo of MHRD |
Hello Friends. Welcome to the tutorial on "Getting Started with Strings". |
Show Slide
Objectives
|
At the end of this tutorial, you will learn to,
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to
If not, see the pre-requisite Python tutorials on this website. |
Show Slide
What are Strings? In Python, any character within a single/ double/ triple quotes is a string.
'This is a string' “This is a string too” ‘’’This can be a large string’’’ |
What are strings?
In Python, any character within a single/ double/ triple quotes is a string. For example: Single, double, and triple quotes can be used to denote a string as shown here.
|
Show Slide
|
Strings in triple quotes can be written in multiple lines.
|
[Terminal]
ipython3
|
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package.
|
[IPython Terminal]
a = 'Hello, World!' |
Let us clear the terminal.
We can assign a string to a variable called a. Type a is equal to inside any quotes Hello comma World exclamation mark and Press Enter |
[IPython Terminal]
|
Now let us learn string concatenation.
Let us assign strings to variables. |
x = 'Hello'
y = 'World'
|
Type
x is equal to inside quotes Hello Press Enter y is equal to inside quotes World Press Enter |
z = x + ', ' + y + '!'
print(z) |
Now, let us add the two strings.
Press Enter print inside parentheses z Press Enter Here x and y are string variables.
|
Highlight + | The addition operation performs the concatenation of two strings. |
Highlight the output | Here we can see the output of string concatenation. |
[IPython Terminal]
Press Up arrow till we get x
|
Let us clear the terminal
What if we multiply a string with an integer? Let us find out! Recall x string. Press Enter x multiplied by 5 Press Enter |
Show Ipython Terminal
|
The string Hello is repeated 5 times after multiplying. |
Show Slide
|
Pause the video. Try this exercise and then resume the video.
|
[IPython Terminal]
print(s) |
Switch to the terminal for solution.
s is equal to inside quotes two percentages plus inside quotes hyphen multiplied by twenty plus inside quotes two percentages Press Enter print inside parentheses s Press Enter |
Let's now look at accessing individual elements of strings.
| |
[IPython Terminal]
Press Up arrow till we get a a[0]
|
Let us clear the terminal.
Recall a. Press Enter. Type, a inside square brackets zero press enter, gives us the first character of the string. |
The indexing starts from 0 and goes up to (n-1).
| |
[IPython Terminal]
a[-2] |
a inside square brackets minus one gives us the last element of the string i.e. exclamation mark
|
Show Slide
Exercise 2 s = “Hello World” what is the output of s[-5] s[-10] s[-15] s[15]
|
Pause the video. Try this exercise and then resume the video.
s[-5] s[-10] s[-15] s[15] |
[Ipython Terminal]
s = "Hello World” s[-5] |
Switch to the terminal.
Let us clear the terminal. Type s is equal to inside quotes Hello World press Enter s inside square brackets minus five gives W Similarly we will get e for s[-10]. |
[Ipython Terminal]
s[-15] |
s inside square brackets minus 15 gives us an IndexError. Since the string given to us is only 11 characters long. |
[Ipython Terminal]
s[15] |
s inside square brackets 15 again gives IndexError for the same reason. |
[Ipython Terminal]
Press Up arrow till we get x x[0] = 'B'
|
Let us attempt to change one of the characters in a string.
Let us clear the terminal. Recall x Press Enter Let us manipulate x. x inside square brackets zero equal to inside quotes B press Enter |
Highlight the error | Why do we get an error?
Initially the value of x[0] is ‘H’. Now we are trying to assign another value ‘B’ to x[0]. Recall, strings cannot be modified after it is created.
|
Show slide | We can split and join the strings by using the functions :
split() and join()
|
Show slide
Syntax for join() str.join(sequence) |
To join a string, the syntax is
str dot join inside parentheses sequence |
[Ipython Terminal]
s = “-”seq = ("a", "b", "c"); print (s.join( seq ))
|
Switch to the terminal.
Let us clear the terminal Type s is equal to inside quotes hyphen press Enterseq is equal to inside parentheses a comma b comma c press Enter print s dot join inside parentheses seq press Enter |
Highlight output | We will get output as a hyphen b hyphen c
It has joined a, b, c with hyphen. |
Show slide syntax for split()
|
To split a string, syntax is
str dot split open and close parentheses |
[Ipython Terminal]
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";print (str.split( ))
|
Switch to the terminal
str is equal to type as shown press Enterprint str dot split open and close parentheses press Enter |
Highlight output | We get the output like this.
It has split the string at spaces. |
Show Slide
Summary slide
|
This brings us to the end of this tutorial.
In this tutorial, we have learnt to,
|
Show Slide
Evaluation
s=”Hello” t=”World” r=2
|
Here are some self assessment questions for you to solve
They are assigned with the values as shown below: s=”Hello” t=”World” r=2
|
Show Slide
Solutions
|
And the answers,
HelloHelloWorldWorld
|
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. |
Show Slide
Textbook Companion |
FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Show Slide
Thank You |
This is _________ from IIT Bombay signing off.
Thank you. |