Strings & Lists in Python
Grade 9 – 2027
Unit 1: Artificial Intelligence and Programming Languages
Lesson 3
Introduction
Python provides several useful ways to store and work with data. Two important data types are Strings and Lists.
Strings are used to store text, while lists are used to store multiple values in one variable.
Watch the Lesson
Watch the lesson first, then use the explanation and examples below to review the main Python concepts.
Learning Objectives
Explain what a string is and how it is created in Python.
Access individual characters using indexing.
Explain what a list is and how it stores multiple values.
Recognize the differences between strings and lists.
1. What is a String?
A String is a sequence of characters used to represent text in Python.
A string can contain letters, numbers, spaces, and special characters.
2. Creating Strings
Example 1
The variable name stores the string "Ahmed".
Example 2
The variable subject stores the text "ICT".
3. String Indexing
Python gives each character in a string an index. Indexing starts from 0.
Consider this string:
| Character | P | Y | T | H | O | N |
|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
Therefore:
The output is:
4. Negative Indexing
Python also allows negative indexes. The last character has index -1.
Output:
5. What is a List?
A List is a collection that can store multiple values in one variable.
Lists are written using square brackets [ ].
The list above contains four items.
6. Creating Lists
List of Numbers
List of Names
Mixed Data
7. List Indexing
Like strings, list items have indexes starting from 0.
Output:
8. Changing a List Item
One important difference between a list and a string is that list elements can be changed.
Output:
9. Adding Items with append()
The append() method adds a new item to the end of a list.
Output:
10. Adding Items with insert()
The insert() method adds an item at a specific position.
Output:
11. Strings vs Lists
| String | List |
|---|---|
| Stores text. | Stores multiple items. |
| Written using quotation marks. | Written using square brackets. |
| Example: "Python" | Example: ["Python", "Java"] |
| Characters can be accessed by index. | Items can be accessed by index. |
| Strings are immutable. | Lists are mutable. |
12. Practical Python Example
We can combine strings and lists in a simple program.
This program stores a student's name in a string and several subjects in a list.
13. Important Points to Remember
- A string is a sequence of characters.
- A list is a collection of items.
- String indexing starts from 0.
- List indexing also starts from 0.
- The last item can be accessed using index -1.
- append() adds an item to the end of a list.
- insert() adds an item at a specific position.
- Lists are mutable, while strings are immutable.
Quick Review
- String: A sequence of characters used to store text.
- List: A collection of multiple values.
- Index: The position of an item or character.
- Python indexing starts at 0.
- append() adds an item to the end of a list.
- insert() adds an item at a specific position.
Interactive Quiz
Choose the best answer for each question. You must answer all questions before submitting.
No comments:
Post a Comment