MASTER YOUR LOGIC BUILDING:Phase 5 - Category 1: Basic String Handling
Mastering phase 5 - category 1: basic string handling concepts and implementation.
Basic String Handling
Why This Matters
Think of a string like a bead necklace — each bead is one character, and they stay in a fixed order. Your phone password, a username, a chat message — all of these are strings. Before you can count vowels, check palindromes, or clean up text, you need to feel comfortable reading and working with strings one character at a time.
What You'll Learn
- How to create strings and find their length
- How to grab the first, last, or any character by position
- How to change case and join strings together
- How to loop through a string character by character
Your First String
name = "Python"
print(len(name)) # 6 characters
print(name[0]) # 'P' — first character (index starts at 0)
print(name[-1]) # 'n' — last character
Single quotes and double quotes both work: 'hello' and "hello" are the same.
Key Ideas (Keep These in Mind)
1. Indexing starts at 0
s[0]→ first characters[-1]→ last characters[1:4]→ characters from index 1 up to (but not including) 4
2. Strings are immutable
You cannot change a character in place. Instead, you build a new string:
word = "cat"
new_word = word[0] + "a" + word[2] # "ca" + "a" + "t" → "caat"
3. Useful built-in tools
text = " Hello World "
print(text.upper()) # " HELLO WORLD "
print(text.lower()) # " hello world "
print(text.strip()) # "Hello World" — trims spaces from both ends
Step-by-Step: First and Last Character
Given "Schoolabe", how do you get 'S' and 'e'?
- Check the string is not empty:
len(s) > 0 - First character:
s[0] - Last character:
s[-1]
s = input("Enter a word: ")
if len(s) > 0:
print("First:", s[0])
print("Last:", s[-1])
else:
print("Empty string!")
Loop Through Every Character
for char in "code":
print(char)
# c
# o
# d
# e
This pattern shows up everywhere — counting letters, finding digits, building a new string.
Common Mistakes & Tips
- Forgetting zero-based indexing — the 5th character is at index
4, not5 - Crashing on empty strings — always check
len(s) > 0before usings[0] - Trying to modify in place —
s[0] = 'X'causes an error; build a new string instead - Tip: Use
s[-1]for the last character instead ofs[len(s)-1]— it's shorter and clearer
Practice Focus
Try these small challenges:
- Print the length of any word the user types
- Print the first and last character (handle empty input)
- Convert a sentence to ALL CAPS and all lowercase
- Loop through a string and print each character on its own line
Master these basics now — every string problem in the next chapters builds on them!
Hands-on Examples
String Length
# Take string input
s = input("Enter a string: ")
# Get length
length = len(s)
print(f"Length: {length}")Use len() function to get the number of characters in the string.
Related Page
🔗Related Content
- 💻
Phase 5 - Practice Problems
Practice Phase 5 concepts with hands-on coding problems
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with assessment questions
- ➡️
Phase 6 - Get Started
Continue to Phase 6 after mastering Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum