← Back to Python Course | Chapter 5: Strings | Lesson 1 of 6

Python Strings Introduction

Creating Strings

A string is an ordered sequence of characters, and Python lets you create one with single quotes, double quotes, or triple quotes — all three produce the exact same str type, so the choice comes down to convenience, like avoiding escaping an apostrophe.

Example: Creating Strings

python
s1 = 'single'
s2 = "double"
s3 = """triple"""
print(type(s1), type(s2), type(s3))

Accessing Characters

Square-bracket indexing, like text[0], retrieves a single character at that zero-based position; Python also supports negative indices, so text[-1] retrieves the last character without needing to know the string's length first.

Example: Accessing Characters

python
text = "Python"
print(text[0])
print(text[-1])

String Length

len(text) returns the total character count of a string, including spaces and punctuation — a value you'll reach for constantly when validating input length or iterating with an index-based loop.

Example: String Length

python
text = "Hello, World!"
print(len(text))

Multiline Strings

Triple-quoted strings ("""...""") can span multiple lines exactly as typed, preserving line breaks — the standard way to embed a block of formatted text, a docstring, or a multi-line message without manual \n characters.

Example: Multiline Strings

python
message = """Line one
Line two
Line three"""
print(message)

Immutability of Strings

Once created, a Python string can never be modified in place — operations like .upper() or .replace() don't change the original string, they return a brand-new one, leaving the original untouched in memory.

Example: Immutability of Strings

python
text = "hello"
upper_text = text.upper()
print(text)
print(upper_text)
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.