Lesson 6 of 30
String Manipulation
Slicing, concatenation, formatting, and the most useful string methods.
String Basics
Strings in Python can be enclosed in single ' or double " quotes, or triple quotes for multi-line text:
s1 = 'Hello'
s2 = "World"
s3 = """This is
a multi-line
string."""
Indexing and Slicing
word = "Python"
print(word[0]) # P (first character)
print(word[-1]) # n (last character)
print(word[0:3]) # Pyt
print(word[::-1]) # nohtyP (reversed)
Useful String Methods
s = " hello, world! "
print(s.strip()) # "hello, world!"
print(s.upper()) # " HELLO, WORLD! "
print(s.lower()) # " hello, world! "
print(s.replace("world", "Python"))
print(s.split(",")) # [' hello', ' world! ']
print(s.count("l")) # 3
print(s.find("world")) # 9
print(s.startswith(" h")) # True
String Formatting (f-strings)
name = "Charlie"
score = 87.5
grade = "B+"
print(f"Student: {name:<10} Score: {score:6.1f} Grade: {grade}")
# Student: Charlie Score: 87.5 Grade: B+
ℹ️ Strings are Immutable
You cannot change individual characters in a string. Methods like upper() return a new string — the original is untouched.