Python Chapter 2: Strings

Master String Manipulation with Interactive Examples

String Basics

1. Creating Strings

Strings in Python can be created using single quotes, double quotes, or triple quotes:

# Single quotes name = 'Alice' # Double quotes message = "Hello, World!" # Triple quotes for multi-line strings paragraph = """This is a multi-line string that spans several lines."""

2. String Indexing

Each character in a string has a position (index). Python uses zero-based indexing:

Positive Indexing

text = "Python" print(text[0]) # 'P' print(text[3]) # 'h'

Negative Indexing

text = "Python" print(text[-1]) # 'n' print(text[-3]) # 'h'

3. String Immutability

Important: Strings in Python are immutable. This means once a string is created, you cannot change its characters. Any operation that seems to modify a string actually creates a new string.
text = "Hello" # text[0] = 'h' # This will cause an error! # Instead, create a new string: new_text = 'h' + text[1:] # 'hello'

4. Escape Characters

Common Escapes

\n # Newline \t # Tab \\ # Backslash \' # Single quote \" # Double quote

Example Usage

text = "Line 1\nLine 2" path = "C:\\Users\\Documents" quote = "She said \"Hi!\""

5. String Operations

Concatenation

first = "Hello" last = "World" result = first + " " + last # "Hello World"

Repetition

laugh = "ha" * 3 # "hahaha" separator = "-" * 20 # "--------------------"

String Explorer

Type Python string operations and see the results instantly. Try examples like:

"Hello".upper() "WORLD".lower() "Python"[0:3] "ha" * 3 "Hello" + " World"
Ready to execute Python string operations...

String Slicing Visualizer

Visualize how string slicing works with positive and negative indices.

Slicing Syntax: string[start:end:step]
- start: where to begin (inclusive)
- end: where to stop (exclusive)
- step: how many characters to skip

Common Slicing Patterns:

First 3 characters

"Python"[0:3] # "Pyt"

From index 2 to end

"Python"[2:] # "thon"

Reverse string

"Python"[::-1] # "nohtyP"

Every 2nd character

"Python"[::2] # "Pto"

String Methods Playground

Explore common string methods with interactive examples.

String Formatting

1. F-Strings (Recommended - Python 3.6+)

The most modern and readable way to format strings:

name = "Alice" age = 25 message = f"My name is {name} and I am {age} years old." # With expressions price = 19.99 result = f"Total: ${price * 2:.2f}" # "Total: $39.98"

2. .format() Method

The traditional formatting method:

# Positional arguments message = "Hello, {}! You have {} messages.".format("Bob", 5) # Named arguments result = "Name: {name}, Score: {score}".format(name="Alice", score=95) # With indices text = "{0} loves {1} and {1} loves {0}".format("Alice", "Bob")

3. % Operator (Old Style)

The legacy formatting method (still works but less preferred):

name = "Alice" age = 25 message = "My name is %s and I am %d years old." % (name, age) # With formatting pi = 3.14159 result = "Pi is approximately %.2f" % pi # "Pi is approximately 3.14"

Interactive Formatting Demo

String Mastery Quiz

Test your understanding of Python strings!

Course Home