4.1.3 Slicing Substrings

Unit: Unit 4: Strings, Lists, and Tuples • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Extract substrings using slice syntax [start:stop:step]
filename = "monthly_report_2026.pdf"

# 1. Extract prefix "monthly" (indices 0 to 7 exclusive)
prefix = filename[0:7]

# 2. Extract year "2026"
year = filename[15:19]

# 3. Extract extension ".pdf" using negative slice
extension = filename[-4:]

# 4. Reverse the word "Python"
sample = "Python"
reversed_sample = sample[::-1]

print("Prefix:", prefix)
print("Year:", year)
print("Extension:", extension)
print("Reversed sample:", reversed_sample)
Open in Live Interactive Python Sandbox →