Python/C3/Getting-started-with-strings/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello friends and Welcome to the tutorial on "Getting started with strings".
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. Define strings in different ways.
  2. Concatenate strings.
  3. Print a string repeatedly.
  4. Access individual elements of the string.
  5. Learn immutability of strings.


Shift to terminal and start ipython
ipython
Open the terminal and invoke the ipython interpreter by typing ipython
Type in ipython the following and read them as you type
'This is a string'
"This is a string too"
This is a string as well
"""This is also a string"""
'p'
""
So, what are strings? In Python anything within either single quotes or double quotes or triple single quotes or triple double quotes are strings.
"Python's string manipulation functions are very useful" Note that it really doesn't matter how many characters are present in the string. The last example is a null string or an empty string.

Having more than one control character to define strings is handy when one of the control characters itself is part of the string. For example

"""Having more than one control character to define
strings come as very handy when one of the control
characters itself is part of the string."""
By having multiple control characters, we avoid the need for escaping characters -- in this case the apostrophe.

Let us now move on to the triple quoted strings. Let us define multi-line strings without using any escaping. Everything within the triple quotes is a single string no matter how many lines it extends

a = 'Hello, World!' We can assign this string to any variable
a = 'Hello'
b = 'World'
c = a + ', ' + b + '!'
print c
Now 'a' is a string variable. String is a collection of characters. In addition string is an immutable collection which means that the string cannot be modified after it is created. So all the operations that are applicable to any other immutable collection in Python, works on strings as well. Hence we can add two strings
We can add string variables as well as the strings themselves all in the same statement. The addition operation performs the concatenation of two strings.
a = 'Hello'
a * 5
Similarly we can multiply a string with an integer
It gives another string in which the original string 'Hello' is repeated 5 times.
Show Slide 3

Assignment 1

Pause the video here, try out the following exercise and resume the video.

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

Switch to terminal
s = "%% " + "-"*20 + " %%"
print s
Let's now look at accessing individual elements of strings. Since, strings are collections, we can access individual items in the string using the subscripts
a[0] a[0] gives us the first character in the string. The indexing starts from 0 for the first character and goes up to (n-1) for the last character, where 'n' is the total number of characters in a string. We can access the strings from the end using negative indices
a[-1]
a[-2]
a[-1] gives us the last element of the string and a[-2] gives us second element from the end of the string.
Show Slide 4

Assignment 2

Pause the video here, try out the following exercise and resume the video.

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

s[-5]
s[-10]
s[-15]
Switch to terminal
s = "Hello World
s[-5]
s[-5] gives us 'W'
s[-10] s[-10] gives us 'e' and
s[-15] s[-15] gives us an IndexError, as should be expected, since the string given to us is only 11 characters long.
a = 'hello'
a[0] = 'H'
Let us attempt to change one of the characters in a string
As said earlier, strings are immutable. We cannot manipulate a string. Although there are some methods which let us manipulate strings, we will look at them in the advanced session on strings. In addition to the methods that let us manipulate the strings we have methods like split which lets us break the string on the specified separator, the join method which lets us combine the list of strings into a single string based on the specified separator.
Show Slide 5

Summary slide

Let's revise quickly what we have learnt today. In this tutorial we have learnt to,
  1. Define strings in different ways.
  2. Concatenate strings by performing addition.
  3. Repeat a string 'n' number of times by doing multiplication.
  4. Access individual elements of the string by using their subscripts.
  5. Use the concept of immutability of strings.


Show Slide 6

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Write code to assign s, the string ' is called the apostrophe
  2. Given strings s and t, s = "Hello" and t = "World" and an integer r, r = 2. What is the output of s * r + s * t?
  3. How will you change s='hello' to s='Hello'.
    • s[0]= H
    • s[0]='H'
    • strings are immutable,hence cannot be manipulated


Show Slide 7

Solution of self assessment questions on slide

And the answers,

1. The given string can be assigned in this manner

s = "` is called the apostrophe"
  1. The operation s * r + s * t will print each of the two words twice
    HelloHelloWorldWorld
  2. Strings are immutable. Therefore they cannot be manipulated.


Show Slide 8

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika