Python Complex Data Types
By- Mr. Meher J. Bharti
Agenda
Chapter- 3
Understanding Sequences
String
List
Dictionary
Tuple
Set
Definition
String is a sequence which is made up of one or
more UNICODE characters. Here the character
can be a letter, digit, whitespace or any other
symbol. A string can be created by enclosing one
or more characters in single, double or triple
quote.
• >>> str1 = 'Hello World!'
• >>> str2 = "Hello World!”
• >>> str3 = """Hello World! welcome to the
world of Python"""
Accessing Characters in a String
The index of the first character is 0 and the last
character is n-1 where n is the length of the string.
If we give index value out of this range then we get
an IndexError. The index must be an integer
(positive, zero or negative).
String Operations
Concatenation
Repetition
Membership
Slicing
Concatenation
Python allows us to join two strings using
concatenation operator plus which is denoted
by symbol +.
Repetition
Python allows us to repeat the given string using
repetition operator which is denoted by symbol *.
str='HCST'
print(str*3)
HCSTHCSTHCST
Membership
Python has two membership operators 'in' and
'not in'.
The 'in' operator takes two strings and returns
True if the first string appears as a substring in the
second string, otherwise it returns False.
The 'not in' operator takes two strings and
returns True if the first string does not appear as a
substring in the second string, otherwise returns
False.
Slicing
In Python, slicing return some part of a string.
The Syntax is: String_name[ n , m , k]
n: Start Index
m: End Index
k: Step Size
str1[n:m] returns all the characters starting from
str1[n] till str1[m-1].
The numbers of characters in the substring will
always be equal to difference of two indices m and
n, i.e., (m-n).
>>> str1 = 'Hello World!'
#gives substring starting from index 1 to 4
>>> str1[1:5]
'ello'
#gives substring starting from 7 to 9
>>> str1[7:10]
'orl'
#index that is too big is truncated down to the end of
#the string
>>> str1[3:20]
'lo World!'
#first index > second index results in an empty string
>>> str1[7:2]
If the first index is not mentioned, the slice starts
from 0th index.
>>> str1[:5] >>>'Hello'
If the second index is not mentioned, the slicing is
done till the length of the string.
>>> str1[6:] >>>'World!‘
Step size k means every kth character has to be
extracted from the string str1 starting from n and
ending at m-1. By default, the step size is one.
>>> str1[0:10:2] >>>'HloWr'
>>> str1[0:10:3] >>>'HlWl‘
Negative indexes can also be used for slicing.
>>> str1[-6:-1] >>>‘World’
>>> str1[::-1] >>> ???
TRAVERSING A STRING
String Traversal Using for Loop:
str1 = 'Hello World!'
for ch in str1:
print(ch,end = '')
String Traversal Using while Loop:
index = 0
while index < len(str1):
print(str1[index],end = '')
index += 1
STRING METHODS AND BUILT-IN
FUNCTIONS
Exercises on Strings
Program 3.1- Write a program to count the number
of times a character (passed as argument) occurs in
the given string.
Sol- [Link]
Program 3.2- Write a program which take input a
string as a parameter and replaces all vowels in the
string with '*'.
Sol- [Link]
Program 3.3- Write a program to input a string from
the user and print it in the reverse order Without
creating a new string.
Sol- [Link]
Program 3.4- Write a program which reverses a
string passed as parameter and stores the reversed
string in a new string.
Sol- [Link]
Program 3.5- Write a program to check if a string
is a palindrome or not. (A string is called
palindrome if it reads same backwards as forward)
Sol- [Link]
Note down the output
mySubject = "Computer Science"
print(mySubject[0:len(mySubject)])
print(mySubject[-7:-1])
print(mySubject[::2])
print(mySubject[len(mySubject)-1])
print(2*mySubject)
print(mySubject[::-2])
print(mySubject[:3] + mySubject[3:])
print([Link]())
print([Link]('Comp'))
print([Link]())
Computer Science
Scienc
Cmue cec
e
Computer ScienceComputer Science
eniSrtpo
Computer Science
cOMPUTER sCIENCE
True
False
Note down the output
myAddress = "WZ-1,New Ganga Nagar,New Delhi"
print([Link]())
print([Link]())
print([Link]('New'))
print([Link]('New'))
print([Link]('New'))
print([Link](','))
print([Link](' '))
print([Link]('New','Old'))
print([Link](','))
print([Link]('Agra'))
wz-1,new ganga nagar,new delhi
WZ-1,NEW GANGA NAGAR,NEW DELHI
2
5
21
['WZ-1', 'New Ganga Nagar', 'New Delhi']
['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
WZ-1,Old Ganga Nagar,Old Delhi
('WZ-1', ',', 'New Ganga Nagar,New Delhi')
Traceback (most recent call last):
File "H:/HCST/2025 - 2026/EVEN
SEM/PYTHON/LAB/[Link]", line 11, in
<module>
print([Link]('Agra'))
ValueError: substring not found
Summary on Strings
• A string is a sequence of characters enclosed in
single, double or triple quotes.
• Indexing is used for accessing individual
characters within a string.
• The first character has the index 0 and the last
character has the index n-1 where n is the
length of the string. The negative indexing
ranges from -n to -1.
• Strings in Python are immutable, i.e., a string
cannot be changed after it is created.
• Membership operator in takes two strings and
returns True if the first string appears as a
substring in the second else returns False.
Membership operator ‘not in’ does the reverse.
• Retrieving a portion of a string is called slicing.
This can be done by specifying an index range.
The slice operation str1[n:m] returns the part of
the string str1 starting from index n (inclusive)
and ending at m (exclusive).
• Each character of a string can be accessed
either using a for loop or while loop.
• There are many built-in functions for working
with strings in Python.