Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner20 min

Visual reference

Multi-line strings in PythonMulti-line strings in Python
String literals and documentationString literals and documentation
Text and symbols as string dataText and symbols as string data

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.

Python — indexed characters in order

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.

Beaded necklace — sequence analogyBeaded necklace — sequence analogy
Characters in order on a stringCharacters in order on a string
Reading text left to rightReading text left to right
  • Characters are accessed by index
  • The first character is index 0
python
word = "Python"

print(word[0])
print(word[1])
print(word[5])
text
P
y
n

Indexing 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
python
s1 = "Python"
s2 = 'Python'

print(s1)
print(s2)
text
Python
Python

More examples from your notes:

python
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.

123 as int vs "123" as str
python
a = 123
b = "123"

print(type(a))
print(type(b))
text
<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):

python
# address = "Guido Van Rossum
# SyntaxError: unterminated string literal

Use triple quotes for multi-line string data:

python
address = """John Doe
221B Baker Street
London"""

print(address)
text
John Doe
221B Baker Street
London

Your 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
python
print("Hello\nWorld")
text
Hello
World
python
print('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.

python
word = "Python"
# word[0] = "J"
# TypeError: 'str' object does not support item assignment

Correct approach: build a new string:

python
word = "Python"
word = "J" + word[1:]
print(word)
text
Jython

Chapter 8 — Concatenation vs addition

+ means numeric addition for numbers and concatenation for strings.

10 + 20 vs "10" + "20"
python
print(10 + 20)
print("10" + "20")
text
30
1020

In 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).

Positive and negative indices on Python
python
word = "Python"

print(word[0])
print(word[-1])
print(word[1:4])
text
P
n
yth

Practice exercises

  1. Store your name in a variable and print type(name).
  2. Build a three-line address with triple quotes (like your notes’ Guido / Travis examples).
  3. Predict "10" + "20" before running the playground.

Quick knowledge check

  1. Is "123" an integer?
    No — it is a str.

  2. Which quotes allow multi-line strings?
    Triple quotes (""" or ''').

  3. What is "10" + "20"?
    "1020" (concatenation), not 30.

Summary

  • str is 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…