Python Lab Manual: Strings and Data Structures
1. Introduction
This lab introduces Python’s basic text (strings) and data structures including lists, tuples,
and dictionaries. You’ll learn how to create and manipulate each.
2. Strings
Strings are sequences of characters enclosed in quotes. Python provides many operations
and methods for string manipulation.
2.1 Basic String Operations
1 name = " Alice "
2 print ( name [0]) # First character
3 print ( name [ -1]) # Last character
4 print ( name [:3]) # Substring
2.2 String Methods
1 text = " hello world "
2 print ( text . upper () ) # HELLO WORLD
3 print ( text . count ( ’l ’) ) # 3
4 print ( text . replace ( " world " , " Python " ) )
Activity 1: Count Vowels in a String
Write a loop that counts and prints the number of vowels in a given string.
1 sentence = " This is a simple Python loop exercise . "
2 vowel_count = 0
3
4 # Your code here
5
6 print ( " Number of vowels : " , vowel_count )
1
Python Lab Manual Strings and Data Structures
Activity 2: Reverse a String
Using a loop, construct the reverse of a given string without using slicing.
1 word = " Programming "
2 reversed_word = " "
3
4 # Your code here
5
6 print ( " Reversed : " , reversed_word )
3. Lists
Lists are ordered collections of items. Lists are mutable.
3.1 Creating and Accessing Lists
1 fruits = [ ’ apple ’ , ’ banana ’ , ’ cherry ’]
2 fruits . append ( ’ orange ’)
3 fruits . remove ( ’ banana ’)
4 print ( fruits [1])
3.2 Iteration and Sorting
1 numbers = [5 , 3 , 9 , 1]
2 numbers . sort ()
3 for num in numbers :
4 print ( num )
Activity 3: Find Maximum in List (No Built-ins)
1 def find_max ( lst ) :
2 max_num = lst [0]
3 for num in lst :
4 if num > max_num :
5 max_num = num
6 return max_num
Activity 4: Print Elements from a Nested List
Given a list of lists, print each element one by one using nested loops.
1 matrix = [[1 , 2 , 3] , [4 , 5] , [6]]
2
3 # Your code here
4
5 # Output should be :
6 # 1 2 3 4 5 6 ( each on a new line )
2
Python Lab Manual Strings and Data Structures
Activity 5: Input Numbers and Compute Average
Write a program that takes 5 numbers from the user, stores them in a list, and prints their
average.
1 numbers = []
2
3 # Your code here
4. Tuples
Tuples are immutable sequences. They are often used for fixed data.
4.1 Basic Tuple Usage
1 person = ( " Alice " , 25)
2 name , age = person
3 print ( f " { name } is { age } years old . " )
Activity 6: Tuple Concatenation and Length
Given two tuples, concatenate them and print the resulting tuple and its length.
1 tuple1 = (1 , 2 , 3)
2 tuple2 = (4 , 5 , 6)
3
4 # Your code here
Activity 7: List of Student Tuples
Store names and grades in a list of tuples and find the highest grade.
5. Dictionaries
Dictionaries store data as key-value pairs. They are unordered and mutable.
5.1 Dictionary Operations
1 student = {
2 ’ name ’: ’ Alice ’ ,
3 ’ age ’: 22 ,
4 ’ grade ’: ’A ’
5 }
6 print ( student [ ’ name ’ ])
7 student [ ’ grade ’] = ’A + ’
8 student [ ’ major ’] = ’ Eng ’
3
Python Lab Manual Strings and Data Structures
5.2 Iterating Over a Dictionary
1 for key , value in student . items () :
2 print ( f " { key }: { value } " )
Activity 8: Iterating Through a Dictionary
Write code to print all student names and their scores from the dictionary.
1 scores = { " Alice " : 85 , " Bob " : 92 , " Charlie " : 78}
2
3 # Your code here
Activity 9: Using Dictionary Methods
Practice using dictionary methods: get(), keys(), values(), and pop(). What will each of the
methods do?
1 scores = { " Alice " : 85 , " Bob " : 92 , " Charlie " : 78}
2
3 # Your code here
6. Summary Table
Type Mutable Example
String No "hello"
List Yes [1, 2, 3]
Tuple No (1, 2, 3)
Dictionary Yes "a": 1
7. Mini Project: Contact Book
Create a contact book using a dictionary where keys are names and values are phone numbers.
Include options to add, remove, update, and search contacts.