Module 3: Python StringsBeginner15 min read
String Slicing & Indexing
Access string characters with positive and negative indexing, step slicing, and reversal.
Key Takeaways & Core Concepts
- and are immutable (they cannot be modified in place).
start: Starting index (inclusive)stop: Stopping index (exclusive)step: Stride / increment (default is 1)
String Slicing & Indexing
### Python Strings: Slicing and Indexing
In Python, strings are sequences of Unicode characters and are immutable (they cannot be modified in place).
#### 1. Indexing You access individual characters using square brackets []. Python supports both positive (0-based) and negative indexing:
C Code Example
1String: P y t h o n
2Index: 0 1 2 3 4 5
3Negative:-6 -5 -4 -3 -2 -1
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
3 lines
PYTHON Code Example
1lang = "Python"
2print(lang[0]) # 'P'
3print(lang[-1]) # 'n'
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
3 lines
#### 2. Slicing Syntax: string[start:stop:step]
start: Starting index (inclusive)stop: Stopping index (exclusive)step: Stride / increment (default is 1)
PYTHON Code Example
1text = "CodeZov Platform"
2print(text[0:7]) # 'CodeZov'
3print(text[:7]) # 'CodeZov' (starts at 0)
4print(text[8:]) # 'Platform' (to the end)
5print(text[::-1]) # 'mroftalP voZedoC' (reverses string!)
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
5 lines
String Reversal with Slicing
1word = "DEVELOPER"
2reversed_word = word[::-1]
3print("Original:", word)
4print("Reversed:", reversed_word)
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
4 lines
Note: A step of -1 traverses the string backwards from the last character to the first.
Practice & AssessmentPython Programming Tutorial
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 0 correct(0 attempted)