Strings and Lists
Strings and Lists
Strings
Introduction
In python, consecutive sequence of characters is known as a string. An individual
character in a string is accessed using a subscript (index). The subscript should always
be an integer (positive or negative). A subscript starts from 0.
Example
>>>myfirst=“Save Earth”
>>>print myfirst
Save Earth
>>>print myfirst[0]
168
To access the fourth character of the string
>>>print myfirst[3]
>>>print myfirst[-1]
>>h
>>>print myfirst[-3]
String A H E L L O
Positive Index 0 1 2 3 4
Negative Index -5 -4 -3 -2 -1
Subscript 0 or –ve n(where n is length of the string) displays the first element.
Note: Python does not support character data type. A string of size 1 can be treated as
characters.
169
Creating and initializing strings
A literal/constant value to a string can be assigned using a single quotes, double quotes
or triple quotes.
Example
As shown in example 2, to include the single quote within the string it should be
preceded by a backslash.
170
In the above example, backslash (\) is used as an escape sequence. An escape
sequences is nothing but a special character that has a specific function. As shown
above, backslash (\) is used to escape the double quote.
Example
>>>raw_input()
Right to education
„Right to education‟
As soon as the interpreter encounters raw_input method, it waits for the user to
key in the input from a standard input device (keyboard) and press Enter key. The
input is converted to a string and displayed on the screen.
Note: raw_input( ) method has been already discussed in previous chapter in detail.
Example
171
Python interpreter was not able associate appropriate data type with the entered
data. So a NameError is shown. The error can be rectified by enclosing the given
input i.e. hello in quotes as shown below
Example
>>>str='honesty'
>>>str[2]='p'
Traversing a string
Traversing a string means accessing all the elements of the string one after the other by
using the subscript. A string can be traversed using: for loop or while loop.
String traversal using for loop String traversal using while loop
A=‟Welcome‟ A=‟Welcome‟
>>>for i in A: >>>i=0
print i >>>while i<len(A)
W print A[i]
172
e i=i+1
l W
c e
o l
m c
e o
m
e
Strings Operations
173
string on the left hand side „Save Earth Save Earth Save Earth
times the value on right ‟
hand side.
174
More on string Slicing
Consider the given figure
String A S A V E E A R T H
Positive Index 0 1 2 3 4 5 6 7 8 9
Example
>>>A=‟Save Earth‟
av
The print statement prints the substring starting from subscript 1 and ending at
subscript 3 .
Example
>>>print A[3:]
„e Earth‟
Omitting the second index, directs the python interpreter to extract the substring till the
end of the string
Example
>>>print A[:3]
Sav
Omitting the first index, directs the python interpreter to extract the substring before
the second index starting from the beginning.
175
Example
>>>print A[:]
„Save Earth‟
Omitting both the indices, directs the python interpreter to extract the entire string
starting from 0 till the last index
Example
>>>print A[-2:]
„th‟
For negative indices the python interpreter counts from the right side (also shown
above). So the last two letters are printed.
Example
>>>Print A[:-2]
„Save Ear‟
Omitting the first index, directs the python interpreter to start extracting the substring
form the beginning. Since the negative index indicates slicing from the end of the string.
So the entire string except the last two letters is printed.
Note: Comparing strings using relational operators has already been discussed in the
previous chapter
176
upper case >>>print str.capitalize()
Welcome
177
contains only numbers. false
Otherwise it returns False.
178
right of the string. Teach India Movement
179
LOWER
Note: In the table given above, len( ) is a built in function and so we don‟t need
import the string module. For all other functions import string statement is required
for their successful execution.
180
Let‟s discuss some interesting strings constants defined in string module:
string.ascii_uppercase
Example
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_lowercase
Example
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
string.ascii_letters
The command displays a string containing both uppercase and lowercase characters.
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits
>>> string.digits
'0123456789'
string.hexdigits
>>> string.hexdigits
'0123456789abcdefABCDEF'
181
string.octdigits
>>> string.octdigits
'01234567'
string.punctuations
>>> string.punctuations
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}-'
string.whitespace
The command displays a string containing all ASCII characters that are considered
whitespace. This includes the characters space, tab, linefeed, return, formfeed, and
vertical tab.
>>> string.whitespace
'\t\n\x0b\x0c\r '
string.printable
The command displays a string containing all characters which are considered printable
like letters, digits, punctuations and whitespaces.
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!
"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}- \t\n\r\x0b\x0c'
Note: Import string module to get the desired results with the commands mentioned
above.
182
Programs using string functions and operators
defpalin():
l=len(str)
p=l-1
index=0
while (index<p):
if(str[index]==str[p]):
index=index+1
p=p-1
else:
break
else:
def lettercount():
word = 'pineapple'
count = 0
if letter == 'p':
count = count + 1
print(count)
183
Regular expressions and Pattern matching
A regular expression is a sequence of letters and some special characters (also called
meta characters). These special characters have symbolic meaning. The sequence
formed by using meta characters and letters can be used to represent a group of
patterns.
For example
str= “Ram$”
The pattern “Ram$” is known as a regular expression. The expression has the meta
character „$‟. Meta character „$‟ is used to match the given regular expression at the end
of the string. So the regular expression would match the string „SitaRam‟ or „HeyRam‟
but will not match the string „Raman‟.
string1='SitaRam' string1='SitaRam'
if if re.search('Sita$',string1):
re.search('Ram$',string1): print "String Found"
print "String Found" else :
else : print" No Match"
print" No Match" Output
Output: No Match
String Found
As shown in the above examples, Regular expressions can be used in python for
matching a particular pattern by importing the re module.
184
Now let‟s learn how the meta characters are used to form regular expressions.
185
6 ? Used to specify that the previous wate?r
character can be matched either The regular expression
once or zero times would only match strings
like watr or water
re.compile()
The re.compile( ) function will compile the pattern into pattern objects. After the
compilation the pattern objects will be able to access methods for various operations
like searching and subsitutions
Example
import re
p=re.compile(„hell*o‟)
re.match()
The match function is used to determine if the regular expression (RE) matches at the
beginning of the string.
re.group()
Example
>>>P=re.compile(„hell*o‟)
>>>m.group()
„hello‟
186
re.start()
re.end()
re.span()
The span function returns the tuple containing the (start, end) positions of the match
Example
>>> import re
>>> P=re.compile('hell*o')
>>> m.start()
>>> m.end()
>>> m.span()
(0, 5)
re.search()
The search function traverses through the string and determines the position where the
RE matches the string
Example
>>> m.start()
15
>>> m.end()
187
20
>>> m.group()
'hello'
>>> m.span()
(15, 20)
Re.findall()
The function determines all substrings where the RE matches, and returns them as a list.
Example
>>> m
['hello', 'hello']
re.finditer()
The function determines all substrings where the RE matches, and returns them as an
iterator.
Example
>>> m
print match.span()
(0, 5)
(24, 29)
188
Script 1: Write a script to determine if the given substring is present in the string.
def search_string():
import re
substring='water'
search1=re.search(substring,'Water water everywhere but not a drop to drink')
if search1:
position=search1.start()
print "matched", substring, "at position", position
else:
print "No match found"
Script 2: Write a script to determine if the given substring (defined using meta
characters) is present in the given string
def metasearch():
import re
p=re.compile('sing+')
search1=re.search(p,'Some singers sing well')
if search1:
match=search1.group()
index=search1.start()
lindex=search1.end()
print "matched", match, "at index", index ,"ending at" ,lindex
else:
print "No match found"
189
EXERCISE
1. Input a string “Green Revolution”. Write a script to print the string in reverse.
2. Input the string “Success”. Write a script of check if the string is a palindrome or
not
3. Input the string “Successor”. Write a script to split the string at every occurrence of
the letter s.
4. Input the string “Successor”. Write a script to partition the string at the occurrence
of the letter s. Also Explain the difference between the function split( ) and
partition().
22
333
4444
55555
6. What will be the output of the following statement? Also justify for answer.
>>> str.replace('o','*')
>>>str.istiltle()
190
10. Write a program to print alternate characters in a string. Input a string of your own
choice.
11. Input a string „Python‟. Write a program to print all the letters except the letter‟y‟.
i) To replace all the occurrences of letter „a‟ in the string with „*‟
def metasearch():
import re
p=re.compile('sing+')
if search1:
match=search1.group()
index=search1.start()
lindex=search1.end()
else:
191
print "No match found"
What will be the output of the above script if search() from the re module is
replaced by match () of the re module. Justify your answer
14. What will be the output of the script mentioned below? Justify your answer.
def find():
import re
p=re.compile('sing+')
print search1
>>> str[5]='p'
192
Chapter 2
Lists
Introduction
Like a String, list also is sequence data type. It is an ordered set of values enclosed in
square brackets []. Values in the list can be modified, i.e. it is mutable. As it is set of
values, we can use index in square brackets [] to identify a value belonging to it. The
values that make up a list are called its elements, and they can be of any type.
We can also say that list data type is a container that holds a number of elements in a
given order. For accessing an element of the list, indexing is used.
It will provide the value at „index+1‟ in the list. Index here, has to be an integer value-
which can be positive or negative. Positive value of index means counting forward from
beginning of the list and negative value means counting backward from end of the list.
Remember the result of indexing a list is the value of type accessed from the list.
0, -size 1st
1, -size +1 2nd
193
2, -size +2 3rd
.
.
.
Please note that in the above example size is the total number of elements in the list.
iv) >>>L4 = [“abc”, 10, 20] # list with different types of elements
You will study about Nested lists in later parts of the chapter.
To change the value of element of list, we access the element & assign the new value.
Example
>>> L1 [2] = 5
[1, 2, 5, 4]
Here, 3rd element of the list (accessed using index value 2) is given a new value, so
instead of 3 it will be 5.
194
L1 0 1 L2 0 Delhi L3
1 2 1 Chennai
2 3 2 Mumbai
3 4
Note: List index works the same way as String index, which is:
An integer value/expression can be used as index.
An Index Error appears, if you try and access element that does not exist in the
list.
An index can have a negative value, in that case counting happens from the end
of the list.
Creating a list
List can be created in many ways:
Example
L5=L1 [:]
>>>print L5
L6 = L1 [0:2]
>>>print L6
Example
>>>n = 5
195
>>>l = range(n)
>>>print l
[0, 1, 2, 3, 4]
Example
>>> print S
In mathematical terms, S can be defined as S = {x2 for: x in (0.....9)}. So, we can say
that list comprehension is short-hand for creating list.
Example
>>> A = [3, 4, 5]
Here B will be created with the help of A and its each element will be thrice of
element of A.
>>> print B
>>>B = [ ]
>>>for i in A
B. append (i*3)
Example
>>> print B
>>>C = [i for i in S if i % 2 = = 0]
196
>>>print C
Example
>>>l = list ( )
>>>print l
[ ] # empty list
Or
L = list (sequence)
Example
>>>print L
[1, 2, 3, 4]
A single new list is created every time, you execute [ ]. We have created many
different lists each using [ ]. But if a list is assigned to another variable, a new list
is not created.
i) A=B=[ ]
Example
>>> print A, B
ii) A=[]
B=A
197
Example
>>> A = [1, 2, 3]
>>> B = A
>>> print A, B
[1, 2, 3] [1, 2, 3]
List Slices
Slice operator works on list also. We know that a slice of a list is its sub-list. For creating
a list slice, we use
[n:m] operator.
>>>print L5 [0]
>>>print L5 [2]
[6, 7, 8]
as the 3rd element of this list is a list. To access a value from this sub-list, we will use
This will return the part of the list from nth element to mth element, including the first
element but excluding the last element. So the resultant list will have m-n elements in it.
>>> L1 [1:2]
will give
[2]
198
Slices are treated as boundaries, and the result will contain all the elements between
boundaries.
Where start, stop & step- all three are optional. If you omit first index, slice starts from
„0‟ and omitting of stop will take it to end. Default value of step is 1.
Example
>>>L2 [0:2]
[“Delhi”, “Chennai”]
Example
>>>list [4:] # will produce a list containing all the elements from 5th position
till end
[50, 60]
Example
>>>list [:3]
>>>list [:]
Example
60
199