Computer Science

Python Strings – Introduction, Indexing & Traversing | Notes with Examples | CBSE Class 11 Computer Science

Class 11 · Computer Science

Python Strings – Introduction, Indexing and Traversing

A string is one of the most commonly used data types in Python. It is a sequence of Unicode characters enclosed within single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """). Strings are widely used for storing names, addresses, passwords, messages, email IDs, and other textual information.


Learning Objectives

  • Understand the concept of strings.
  • Create strings in different ways.
  • Access characters using indexing.
  • Differentiate between positive and negative indexing.
  • Traverse strings using loops.
  • Understand string immutability.

What is a String?

A string is an ordered collection (sequence) of characters. A character may be a letter, digit, space, punctuation mark, or any special symbol.

Examples

name="Rahul"
city='Jaipur'
course="Computer Science"
password="ABC@123"
message="Welcome to Python"

Characteristics of Strings

  • Strings are sequences of characters.
  • Strings are enclosed within quotes.
  • Strings are immutable.
  • Each character has an index.
  • Strings support indexing and slicing.
  • Strings can be traversed using loops.

Creating Strings

Using Single Quotes

name='Python'

Using Double Quotes

name="Python"

Using Triple Quotes

message="""Welcome
to
Python"""

String Indexing

Every character in a string has a unique position called an index. Python supports both positive indexing and negative indexing.

Example

word="PYTHON"
Character P Y T H O N
Positive Index 0 1 2 3 4 5
Negative Index -6 -5 -4 -3 -2 -1

Accessing Characters

word="PYTHON"
print(word[0])
print(word[3])
print(word[-1])
print(word[-4])

Output

P
H
N
T

Accessing First and Last Character

name="Computer"
print(name[0])
print(name[-1])

Output

C
r

Traversing a String Using for Loop

Traversing means accessing each character of the string one by one.

text="Python"
for ch in text:
    print(ch)

Output

P
y
t
h
o
n

Traversing a String Using while Loop

text="Python"
i=0
while i

Output

P
y
t
h
o
n

Finding the Length of a String

The len() function returns the total number of characters in a string, including spaces and special characters.

text="Computer Science"
print(len(text))

Output

16

String Immutability

Strings in Python are immutable, which means their characters cannot be modified after the string is created.

Wrong

name="Python"
name[0]="J"

Result

TypeError

Correct Method

name="Python"
name="Jython"

Real-Life Applications

  • Storing names of students.
  • Saving email addresses.
  • Processing passwords.
  • Reading text files.
  • Displaying messages.
  • Searching words in documents.
  • Generating reports.
  • Processing customer information.

Common Programming Mistakes

1. Accessing an Invalid Index

name="Python"
print(name[10])

Result: IndexError


2. Trying to Modify a Character

name="Python"
name[1]="A"

Result: TypeError because strings are immutable.


3. Forgetting Quotes

name=Python

Result: NameError because Python treats Python as a variable instead of a string.


CBSE Important Programs

  1. Print each character of a string using a for loop.
  2. Print each character using a while loop.
  3. Display the first and last character of a string.
  4. Find the length of a string.
  5. Count the number of characters by traversing the string.
  6. Print characters at even index positions.
  7. Print characters at odd index positions.
  8. Reverse print a string using indexing.

Viva Questions

  1. What is a string?
  2. How are strings created in Python?
  3. What is indexing?
  4. Differentiate between positive and negative indexing.
  5. What is string traversal?
  6. Can strings be modified after creation?
  7. What is meant by string immutability?
  8. Which function returns the length of a string?
  9. Can spaces be stored in a string?
  10. Give two real-life applications of strings.

Exam Tips

  • Remember that indexing starts from 0.
  • Negative indexing starts from -1.
  • Strings are immutable.
  • Use len() to determine the length of a string.
  • Practice traversing strings using both for and while loops.

Quick Revision

  • A string is a sequence of Unicode characters.
  • Strings can be enclosed in single, double, or triple quotes.
  • Each character has a positive and negative index.
  • Strings can be traversed using loops.
  • Strings are immutable and cannot be modified directly.
  • len() returns the total number of characters in a string.