Lesson 05
str — String Data Type
Master Python strings from quotes and multi-line text to escape characters, immutability, indexing, slicing preview, and why "123" is text—not a number.
Visual reference
What you will learn
- Understand
str— what a string really is - Quotes — single, double, and triple quotes
- Sequence — indexing and slicing preview
- Immutability — why characters cannot be changed in place
Chapter 1 — What is a string?
str is a pre-defined class and a sequence data type.
Purpose: store string data, text, alphanumeric data, numeric characters, and special symbols inside single quotes, double quotes, or triple quotes.
Definition: a string is a collection of characters enclosed within quotes. Each character has a position called an index.
Real-world examples
| Data | Type |
|---|---|
"Mahesh" |
str |
"DWEL-146" |
str |
"A+" |
str |
"admin@example.com" |
str |
"₹12,500" |
str |
From your notes: "123456", "Python3.11", "123$456_abc", and "@#$%^&8912" are all str.
Chapter 2 — Why is it called a sequence type?
Sequence means order matters. Think of a beaded necklace — each bead is one character; position is preserved.
- Characters are accessed by index
- The first character is index
0
word = "Python"
print(word[0])
print(word[1])
print(word[5])P
y
nIndexing and slicing are covered in depth in the String Handling module; here is a preview only.
Chapter 3 — Three ways to create strings
Your notes list two kinds of string data: single-line and multi-line. Python offers three quotation styles:
| Style | Example | Use |
|---|---|---|
| Single quotes | 'Python' |
Short text |
| Double quotes | "Python" |
Same as single quotes |
| Triple quotes | """Text""" or '''Text''' |
Multi-line paragraphs |
s1 = "Python"
s2 = 'Python'
print(s1)
print(s2)Python
PythonMore examples from your notes:
print("A", type("A"))
print("123456", type("123456"))
print("Python3.11", type("Python3.11"))Chapter 4 — Digits inside quotes are not numbers
Quotes tell Python: treat these digits as symbols, not quantities.
a = 123
b = "123"
print(type(a))
print(type(b))<class 'int'>
<class 'str'>| Value | Type |
|---|---|
123 |
int |
"123" |
str |
"007" |
str |
"9876543210" |
str |
Phone numbers and roll numbers with leading zeros are usually stored as str, not int.
Chapter 5 — Multi-line strings
Single-line "..." and '...' cannot span rows (from your notes):
# address = "Guido Van Rossum
# SyntaxError: unterminated string literalUse triple quotes for multi-line string data:
address = """John Doe
221B Baker Street
London"""
print(address)John Doe
221B Baker Street
LondonYour notes also use addresses with Guido Van Rossum / Travis Oliphant blocks — same idea. Triple quotes preserve line breaks exactly as written.
Try it in the playground
Edit the address = """...""" block in the starter code: change names and lines, run again, and see multi-line output.
Chapter 6 — Escape characters
Sometimes you need special characters inside strings:
| Escape | Meaning |
|---|---|
\n |
New line |
\t |
Tab |
\" |
Double quote inside double-quoted string |
\' |
Single quote inside single-quoted string |
\\ |
Backslash |
print("Hello\nWorld")Hello
Worldprint('He said "Hello"')
print("He said \"Hello\"")Chapter 7 — Strings are immutable
Immutable means you cannot change a character in place after the string is created.
word = "Python"
# word[0] = "J"
# TypeError: 'str' object does not support item assignmentCorrect approach: build a new string:
word = "Python"
word = "J" + word[1:]
print(word)JythonChapter 8 — Concatenation vs addition
+ means numeric addition for numbers and concatenation for strings.
print(10 + 20)
print("10" + "20")30
1020In the playground, change s = "Python" (add your own text) and use len(s), s[0], and type(s).
Chapter 9 — Preview of indexing and slicing
Every character has a positive index (from 0) and a negative index (from the end).
word = "Python"
print(word[0])
print(word[-1])
print(word[1:4])P
n
ythPractice exercises
- Store your name in a variable and print
type(name). - Build a three-line address with triple quotes (like your notes’ Guido / Travis examples).
- Predict
"10" + "20"before running the playground.
Quick knowledge check
-
Is
"123"an integer?
No — it is astr. -
Which quotes allow multi-line strings?
Triple quotes ("""or'''). -
What is
"10" + "20"?
"1020"(concatenation), not30.
Summary
stris Python’s text type — an ordered sequence of characters.- Use
'...'or"..."for single-line text;"""..."""for multi-line. - Digits in quotes are characters, not numeric types.
- Strings are immutable — create new strings instead of assigning to
word[0]. +concatenates strings; indexing/slicing are previewed here and expanded later.
Next lesson: bytes — immutable byte sequences and the difference between str and bytes.
Loading playground…