Chapter 8: Collections and String Manipulation
Learn how Python manages groups of data using collections and processes text using powerful string manipulation techniques.
[ ]
( )
{key:value}
{ }
8.1 Chapter Overview
In Python, collections are used to store multiple values in a single variable, while string manipulation is used to process and control text data. These two skills are very important for developing real-world applications such as student records systems, attendance systems, course registration systems, report generation tools, chatbot applications, and data cleaning programs.
This chapter combines collection data types and string manipulation so learners can understand how to store, organize, search, update, format, and display data professionally.
8.2 Chapter Objectives
- Understand the purpose of collection data types in Python.
- Create and manage lists, tuples, dictionaries, and sets.
- Use loops to process collection items.
- Understand the purpose of string manipulation.
- Apply common string methods such as upper(), lower(), title(), replace(), split(), and strip().
- Use strings and collections together in practical programs.
- Develop simple applications for student, course, and text processing tasks.
Learning Outcomes
- Differentiate between list, tuple, dictionary, and set.
- Store and process multiple values using Python collections.
- Clean and format text using Python string methods.
- Create practical Python applications using collections and strings.
8.3 Introduction to Python Collections
A collection is a data type that stores more than one value. Instead of creating many separate variables, programmers can group related values together.
| Collection | Symbol | Purpose | Example |
|---|---|---|---|
| List | [ ] | Stores ordered and changeable data. | ["Python", "AI", "Data Science"] |
| Tuple | ( ) | Stores ordered data that should not change. | ("CPP101", "AI201") |
| Dictionary | { } | Stores data using key-value pairs. | {"name": "Amin", "age": 20} |
| Set | { } | Stores unique values only. | {"Python", "AI", "Python"} |
8.4 Lists
A list is an ordered and changeable collection. Lists are suitable when data needs to be updated, added, removed, or processed repeatedly.
Creating a List
courses = ["Python", "AI", "Data Science", "Cyber Security"] print(courses)
Accessing List Items
courses = ["Python", "AI", "Data Science"] print(courses[0]) print(courses[1])
Python
AI
Adding and Removing Items
courses = ["Python", "AI"]
courses.append("Data Science")
courses.remove("AI")
print(courses)
['Python', 'Data Science']
8.5 Tuples
A tuple is similar to a list, but it cannot be changed after creation. Tuples are useful for fixed information such as course codes, centre details, months, or weekdays.
course_codes = ("CPP101", "AI201", "DS301")
print(course_codes)
print(course_codes[0])
('CPP101', 'AI201', 'DS301')
CPP101
8.6 Dictionaries
A dictionary stores data in key-value pairs. Dictionaries are excellent for structured records such as student profiles, employee details, course information, and product records.
Creating a Dictionary
student = {
"name": "Amin",
"age": 20,
"course": "Certified Python Programmer"
}
print(student)
Accessing Dictionary Values
print(student["name"]) print(student["course"])
Updating Dictionary Values
student["course"] = "AI Application Development" student["email"] = "amin@example.com" print(student)
8.7 Sets
A set stores unique values only. It is useful when duplicate data should be removed automatically.
skills = {"Python", "AI", "Python", "Data Science"}
print(skills)
The duplicate value Python appears only once in the set.
Set Operations
group_a = {"Python", "AI", "Excel"}
group_b = {"Python", "Data Science", "Power BI"}
print(group_a.union(group_b))
print(group_a.intersection(group_b))
8.8 Introduction to String Manipulation
String manipulation means working with text data. In Python, a string is a sequence of characters enclosed inside single quotes or double quotes.
student_name = "Amin" course = "Python Programming" print(student_name) print(course)
Amin
Python Programming
8.9 String Indexing and Slicing
Strings can be accessed using index numbers. Python indexing starts from 0.
Indexing
course = "Python" print(course[0]) print(course[1])
P
y
Slicing
course = "Python Programming" print(course[0:6]) print(course[7:18])
Python
Programming
8.10 Common String Methods
| Method | Purpose | Example |
|---|---|---|
| upper() | Converts text to uppercase. | "python".upper() |
| lower() | Converts text to lowercase. | "PYTHON".lower() |
| title() | Capitalizes each word. | "python programming".title() |
| strip() | Removes extra spaces from beginning and end. | " Python ".strip() |
| replace() | Replaces one text with another. | "Python".replace("Python","AI") |
| split() | Splits text into a list. | "AI,Python".split(",") |
Example
message = " welcome to pdtc python course " print(message.upper()) print(message.lower()) print(message.title()) print(message.strip())
8.11 String Concatenation and Formatting
Concatenation means joining strings together. Python also supports f-strings for professional formatting.
Concatenation
first_name = "Amin" last_name = "Rahman" full_name = first_name + " " + last_name print(full_name)
f-String Formatting
name = "Amin"
course = "Python"
print(f"Student {name} is enrolled in {course}.")
Student Amin is enrolled in Python.
8.12 Using split() with Collections
The split() method is very useful because it converts a string into a list.
skills_text = "Python,AI,Data Science,Cyber Security"
skills_list = skills_text.split(",")
print(skills_list)
['Python', 'AI', 'Data Science', 'Cyber Security']
8.13 Practical Example: Student Profile Application
This example uses a dictionary for student details, a list for skills, and string methods to clean and format input.
student_name = input("Enter student name: ").strip().title()
course = input("Enter course name: ").strip().title()
skills_text = input("Enter skills separated by comma: ")
skills = skills_text.split(",")
student = {
"name": student_name,
"course": course,
"skills": skills
}
print("----- Student Profile -----")
print("Name:", student["name"])
print("Course:", student["course"])
print("Skills:", student["skills"])
Enter student name: amin rahman
Enter course name: python programming
Enter skills separated by comma: Python,AI,Excel
Name: Amin Rahman
Course: Python Programming
Skills: ['Python', 'AI', 'Excel']
8.14 Practical Example: Remove Duplicate Skills
This example uses a list and set together to remove duplicate skill names.
skills = ["Python", "AI", "Python", "Excel", "AI"] unique_skills = set(skills) print(unique_skills)
{'Python', 'AI', 'Excel'}
8.15 Common Beginner Mistakes
| Mistake | Problem | Correction |
|---|---|---|
| Using wrong brackets | Confusing lists, tuples, sets and dictionaries. | Use [ ] for lists, ( ) for tuples and { } for dictionaries/sets. |
| Changing tuple values | Tuples are immutable. | Use list if the values must change. |
| Forgetting string input cleaning | Extra spaces may affect output. | Use strip() to remove unnecessary spaces. |
| Expecting sets to keep order | Sets are unordered. | Use lists if order is important. |
8.16 Hands-On Practice
Activity 1: Course List Manager
Create a list of courses. Add one new course and remove one course. Display the final list.
Activity 2: Student Dictionary
Create a dictionary for a student profile with name, age, course and email. Display each value clearly.
Activity 3: String Formatter
Ask the user to enter their full name. Display the name in uppercase, lowercase and title case.
Activity 4: Skills Splitter
Ask the user to enter skills separated by commas. Convert the input into a list using split().
Mini Project: Student Skills Profile
Create an interactive Python program that asks for student name, course and skills. Format the name and course using string methods, split skills into a list, remove duplicate skills using a set, and display the final profile.
8.17 Final Assessment Quiz
Answer the following questions. Correct Answer = +1 Mark Wrong Answer = -0.5 Mark
1. A list can store multiple values in one variable.
2. A tuple can be changed after creation.
3. Dictionaries store data using key-value pairs.
4. Sets are useful for removing duplicate values.
5. The upper() method converts text to uppercase.
6. The split() method can convert a string into a list.
7. The strip() method removes extra spaces from the beginning and end of a string.
8. Python string indexing starts from 1.
9. f-strings are used to format output messages.
10. Collections and string methods can be used together in real applications.
Your Score: 0
Final Practical Assessment
Develop an interactive Python program named student_skills_profile.py. The program must ask for student name, course name and skills separated by commas. It must clean and format the name and course, convert skills into a list, remove duplicate skills using a set, and display a clear student profile.
8.18 Chapter Summary
In this chapter, learners studied Python collections and string manipulation. Collections help store and manage multiple values, while string manipulation helps process text data. Together, these skills allow developers to build more practical and interactive Python applications.