Python-3.4.3/C2/Getting-started-with-strings/English

From Script | Spoken-Tutorial
Revision as of 16:52, 7 December 2017 by Trupti (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Python/C2/Getting Started with Strings/English
Title of script: Getting Started with Strings
Author: Trupti Kini
Keywords: Python, Ipython


Visual Cue Narration
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,


  1. Define strings in different ways.
  2. Concatenate strings.
  3. Print a string repeatedly.
  4. Access individual elements of the string.
  1. Learn immutability of strings.


Show slide

System Specifications

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


Show Slide

Pre-requisites

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.

Show Slide

What are Strings?

In Python, any character within a single/ double/ triple quotes is a string.


Example:

'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


What are Strings?

Strings in triple quotes can be written in multiple lines.


We can also define empty strings.


String is an immutable collection.

That is, string cannot be modified after it is created.


String is a collection of characters which cannot be modified after it is created.

[Terminal]

ipython3


Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.


Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.


Type %pylab and press Enter.

[IPython Terminal]

a = 'Hello, World!'

We can assign a string to a variable called a.

Type a is equal to inside any quotes Hello comma World exclamation mark

Press Enter

[IPython Terminal]


Now let us learn string concatenation.

Let us assign strings to variables.

x = 'Hello'

y = 'World'

z = x + ', ' + y + '!'

print(z)

Type

x is equal to inside quotes Hello

Press Enter

y is equal to inside quotes World

Press Enter

Now, let us add the two strings.


z is equal to x plus inside quotes comma plus y plus inside quotes exclamation mark.

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


x * 5

What if we multiply a string with an integer?

Let us find out!

Type

Recall x string.

Press Enter

x multiplied by 5

Press Enter

Show Ipython Terminal


Highlight the output

The string Hello is repeated 5 times after multiplying.
Show Slide


Exercise 1

Pause the video. Try this exercise and then resume the video.


  1. Obtain the string %% --------------------%% (20 hyphens) without typing out all the twenty hyphens.


[IPython Terminal]


s = "%% " + "-"*20 + "%%"

print(s)

Switch to 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

print inside parentheses s

Press Enter

Let's now look at accessing individual elements of strings.


We can access individual elements in the string using the subscripts.

[IPython Terminal]

Press Up arrow till we get a

a[0]


Recall a.

Type,

a inside square brackets zero gives us the first character of the string.


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.

[IPython Terminal]


a[-1]

a[-2]

a inside square brackets minus one gives us the last element of the string i.e. !


a inside square brackets minus two gives us second element from the end of the string i.e. d

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.


Given a string, s which is Hello World, what is the output of

s[-5]

s[-10]

s[-15]

s[15]

[Ipython Terminal]

s = "Hello World

s[-5]

Switch to the terminal.

Type

s is equal to inside quotes Hello World press Enter

s inside square brackets minus five gives us 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'


Highlight the error.

Let us attempt to change one of the characters in a string.

Type,

Recall x

Let us manipulate x.

x inside square brackets zero equal to inside quotes B press Enter


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.


We are trying to change the value of x[0] which was ‘H’ to B'.

We are getting added anan error because, strings are immutable and so cannot be manipulated.


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

str dot join inside parentheses sequence

[Ipython Terminal]

s = “-”seq = ("a", "b", "c"); print (s.join( seq ))


Switch to the terminal.

Type

s is equal to inside quotes hyphen press Enterseq is equal to inside parentheses a,b,c press Enter

print s dot join inside parentheses seq press Enter

Highlight output We will get output as a-b-c

It has joined a, b, c with hyphen.

Show slide syntax for split()


str.split()

To split a string

str dot split open and close parentheses

[Ipython Terminal]

str = "Line1-abcdef \nLine2-abc \nLine4-abcd";print (str.split( ))


str is equal to "Line1-abcdef \nLine2-abc \nLine4-abcd" press Enterprint str dot split open and close parentheses press Enter
Highlight output We get the output ['Line1-abcdef', 'Line2-abc', 'Line4-abcd']

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,


  1. Define strings in different ways.
  • Concatenate strings by performing addition.
  • Repeat a string 'n' number of times by doing multiplication.
  • Access individual elements of the string by using their subscripts.
  • Use the concept of immutability of strings.
  • Define strings in different ways.
  • Concatenate strings.
  • Print a string repeatedly.
  • Access individual elements of the string.



Show Slide Evaluation #
Write code to assign the string ' is called the apostrophe , to a variable s
  1. How will you change s='hello' to s='Hello'.
  2. Change the formatting as in visual cue.The variables s and t are string and r is integer.


s=”Hello”

t=”World”

r=2


What is the output of s * r + t * r?


Here are some self assessment questions for you to solve


#
Write code to assign the string ' is called the apostrophe , to variable s
  1. How will you change s='hello' to s='Hello'.
  2. The variables s and t are string and r is integer.


They are assigned with the values as shown below:

s=”Hello”

t=”World”

r=2


What is the output of s * r + t * r?


Show Slide

Solutions


And the answers, #
The given string can be assigned in this manner


s = " ` is called the apostrophe"

2.Strings cannot be manipulated after it is assigned a value.


3.The operation s * r + t * r will print each of the

two words twice as

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

http://spoken-tutorial.org

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.


Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Trupti