Python18 min read

Python Strings

Learn strings deeply: creation, indexing, slicing, methods, formatting, and common mistakes.

Emily Davis
September 8, 2025
5.8k137

Strings represent text. They are used in names, messages, file paths, and almost every program you write.

The most important concept:

  • a string is a sequence of characters

Creating strings

name = "Lisa"
message = "Hello from Chicago"
multi_line = """This is
a multi-line
string."""

print(name)
print(message)
print(multi_line)

Expected output:

Lisa
Hello from Chicago
This is
a multi-line
string.

Indexing (getting characters)

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

Expected output:

P
n

Slicing (getting part of a string)

text = "Python"
print(text[0:3])
print(text[2:])

Expected output:

Pyt
thon

Strings are immutable (important)

You cannot change a character directly:

text = "Python"
# text[0] = "J"  # this causes an error

Instead, build a new string:

text = "Python"
new_text = "J" + text[1:]
print(new_text)

Expected output:

Jython

Useful string methods

text = "hello world"

print(text.upper())
print(text.capitalize())
print(text.replace("world", "Python"))
print(len(text))

Expected output:

HELLO WORLD
Hello world
hello Python
11

Formatting strings (use f-strings)

name = "Tom"
age = 25
print(f"My name is {name} and I am {age}.")

Expected output:

My name is Tom and I am 25.

Graph: string processing pipeline

flowchart LR
  A["input text"] --> B[clean/transform]
  B --> C[format into message]
  C --> D[print/output]

In the next lesson, you will learn numbers and calculations, including what each operator means and when it is used.

#Python#Beginner#Strings