Python Tutorial 0/52 lessons ~6 min read Lesson 21

    Strings

    Python strings are immutable Unicode sequences.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Python strings are immutable Unicode sequences. Rich method API: split, join, strip, replace, format, f-strings.

    Understanding the topic

    Core concepts to understand:

    • Immutable — every 'edit' creates a new string.
    • f-strings (3.6+) are the modern format.
    • join over += in loops.
    • str vs bytes — Unicode vs raw octets.

    Syntax reference

    Visual flow / code:

    python
    name = "Alice"
    greeting = f"Hello, {name.upper()}!"
    # Split / join
    parts = "a,b,c".split(",") # ['a','b','c']
    joined = "-".join(parts) # 'a-b-c'
    # Strip & replace
    " hi ".strip()
    "hello".replace("l", "L")
    # Multi-line + f-string with format spec
    pi = 3.14159
    print(f"pi = {pi:.2f}")
    # Encode/decode
    b = "café".encode("utf-8")
    s = b.decode("utf-8")

    Execution workflow

    1Strings Workflow
    1 / 4

    Step 1

    Immutable — every 'edit' creates a new string.

    Apply this step while implementing strings in real code.

    Real-world use

    Use f-strings everywhere. Use str.join for building large strings — it's O(n), while += in a loop is O(n²).

    Best practices

    • Prefer f-strings (fastest, clearest).
    • Use join for concatenating many parts.
    • Always specify encoding for bytes ↔ str.

    Common mistakes

    • s += '...' in a loop builds quadratic-time garbage.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Why are strings immutable?
    • f-string vs % vs format?
    • str vs bytes?

    Summary

    In summary: Immutable Unicode. f-strings + join = idiomatic.

    Ready to mark this lesson complete?Track your journey across the entire course.