Strings 💬🔤✏️
Strings are how programs handle text. Every chat, search and message in every app is a string!
Day 14: Strings — How Programs Read and Use Text!
What's a String?
Every message you send on WhatsApp, every search you type on Google, every username on Instagram — all of that is text. In Python, text is called a String. Think of it like a train of letters — "Rohith" is 6 carriages: R, o, h, i, t, h!
Creating a String
name = "Rohith"
message = "Hello World"
city = "Hyderabad"
Always use quotes. Single or double both work. Without quotes Python thinks it's a variable name, not text!
Strings Are Like Lists of Letters
Just like lists, each letter in a string has a position starting from 0!
name = "Rohith"
print(name[0]) # R
print(name[1]) # o
print(name[5]) # h
R is at position 0, o is at 1, h is at 2, i is at 3, t is at 4, h is at 5. Counting always starts from zero!
Useful String Tricks
name = "rohith"
print(len(name))
print(name.upper())
print(name.lower())
print(name.capitalize())
Instagram uses upper() and lower() to make username searches work whether you type "PRIYA" or "priya". Same result!
Joining Strings Together
first = "Rohith"
last = "Builds"
full = first + " " + last
print(full)
Output: Rohith Builds. The + joins strings together. This is called concatenation. WhatsApp uses this to show "Priya is typing..." by joining a name with " is typing..."!
Common Mistakes
Mistake 1 — Forgetting quotes.
name = Rohith
name = "Rohith"
Mistake 2 — Going out of range.
name = "Rohith"
print(name[6])
print(name[5])
Mini Challenge
Mini Challenge
Store your full name as a string. Print how many letters it has using len(). Print it in all capitals using upper(). Then print just the first letter using [0]. You just built a name formatter like every signup form uses!
Quick Quiz
Q: What position is the first letter of any string? A: Position 0 — always start counting from zero!
Q: How do you find how many characters are in a string? A: len(string)!
Q: How do you join two strings together? A: Use the + sign — "Hello" + " " + "World"!
Key Takeaways
Key Takeaways
- Strings are text data — always written inside quotes.
- Each letter has a position starting from 0.
- len() counts characters. upper() makes it capitals. lower() makes it lowercase.
- Use + to join strings together.
- Every chatbot, search engine and AI starts by understanding strings!
Continue Learning with Rohi
You've used your 3 free Rohi questions. Create a free account to continue learning.