Creating Strings
Strings can be created with single quotes, double quotes, or triple quotes for multi-line strings. They all work the same way.
single = 'Hello'
double = "World"
multi = """This is
a multi-line
string"""
print(single)
print(double)
print(multi)
Try it Yourself โ
Indexing and Slicing
Strings are sequences, so you can access individual characters by index (starting from 0) and slice them with the [start:end] syntax.
text = "Python"
print(text[0])
print(text[-1])
print(text[0:3])
print(text[:3])
print(text[3:])
print(text[::-1])
Try it Yourself โ
String Methods
Python strings come with a rich set of built-in methods for transformation, searching, and formatting. They never modify the original string โ they return a new one.
msg = " Hello, Python World! "
print(msg.lower())
print(msg.upper())
print(msg.strip())
print(msg.replace("World", "Universe"))
print(len(msg))
print(msg.split())
Try it Yourself โ
f-Strings
f-Strings let you embed expressions directly inside string literals using curly braces. They're the modern way to format strings in Python.
name = "Alice"
age = 30
score = 95.5
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Score: {score:.1f}%")
print(f"Next year you'll be {age + 1}")
Try it Yourself โ