Curriculum / Python / Lesson 10

Strings in Python

Python strings (str) are sequences of characters, and like lists, they support indexing and slicing.

10 of 11
beginner 📖 5 min read 🎯 1 question

Strings

Python strings (str) are sequences of characters, and like lists, they support indexing and slicing. Python also has one of the most readable ways to format strings: f-strings.

💡 Key insight Python strings are immutable, just like in Java — every 'modifying' method actually returns a brand new string.

Syntax

syntax
name = "text"
name[0]         # first character
name[0:3]       # slice
f"Hello, {name}!"

Example

len() gives the character count, and f-strings (f"...") let you embed variables directly inside the string.

py
name = "CodeyHub"
print(f"Name: {name}")
print("Length:", len(name))
print("Upper:", name.upper())
outputName: CodeyHub Length: 8 Upper: CODEYHUB

Try it yourself

Copy this example into the Python compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What does len("CodeyHub") return?

Back to curriculum