Strings: Working with Text
Slicing, searching, and transforming text — one of the things Python does best.
Text is everywhere
Names, messages, file contents, web pages, user input — a huge amount of real programming is just shuffling text around. Python's text type, the string, comes loaded with tools that make this genuinely pleasant. A string is any text inside quotes, single or double, it doesn't matter which:
greeting = "Hello"
name = 'Ahmedabad'
Use double quotes when your text contains an apostrophe ("it's a cat") and single quotes when it contains double quotes. For text spanning multiple lines, use triple quotes:
poem = """Roses are red,
Violets are blue,
Python is easy,
And so are you."""
Strings are sequences — indexing and slicing
A string is an ordered sequence of characters, and each character has a position number called an index, starting from zero. That zero trips up every beginner once, so burn it in: the first character is at index 0.
word = "PYTHON"
# 012345
print(word[0]) # P (first character)
print(word[1]) # Y
print(word[-1]) # N (negative counts from the end)
print(word[-2]) # O
You can grab a whole range of characters with slicing, using [start:stop]. The start is included, the stop is not:
word = "PYTHON"
print(word[0:3]) # PYT (positions 0, 1, 2 — stop 3 excluded)
print(word[2:]) # THON (from 2 to the end)
print(word[:4]) # PYTH (from start up to 4)
This "stop is excluded" rule is consistent across all of Python — once it clicks for strings, it clicks for lists and everything else too.
The string toolbox: methods
A method is an action you ask a value to perform on itself, written with a dot. Strings have dozens; here are the ones you'll reach for daily:
text = " Hello World "
print(text.upper()) # " HELLO WORLD "
print(text.lower()) # " hello world "
print(text.strip()) # "Hello World" (trims outer spaces)
print(text.replace("World", "Python")) # " Hello Python "
print("Hello World".split()) # ['Hello', 'World'] -> a list of words
An important subtlety: these methods do not change the original string. Strings in Python are immutable — they can never be modified in place. Every method hands you back a brand-new string. So you must capture the result:
name = "mehul"
name.upper() # this result is thrown away!
print(name) # still "mehul"
name = name.upper() # capture it back into the variable
print(name) # "MEHUL"
Searching and checking inside text
You'll constantly ask questions about text. Python makes them read like English:
email = "student@codaiman.com"
print("@" in email) # True — is @ present?
print(email.startswith("std")) # False
print(email.endswith(".com")) # True
print(email.find("@")) # 7 — the index where @ sits
print(len(email)) # 20 — how many characters
The in keyword is wonderfully readable and works everywhere in Python. len() gives the length of almost anything — strings, lists, and more — so it's worth remembering early.
Putting it together: a tiny validator
Let's combine these tools into something useful — a check that a typed email looks roughly valid:
email = input("Enter your email: ").strip().lower()
has_at = "@" in email
has_dot = "." in email
long_enough = len(email) >= 5
if has_at and has_dot and long_enough:
print(f"Thanks! We'll contact you at {email}")
else:
print("That doesn't look like a valid email.")
Look how readable that is. We trim and lowercase the input in one chained line, ask three plain-English questions, and combine them with and. This is the real flavour of Python: small, clear pieces that snap together. We used an if statement there to make a decision — and decisions are exactly what the next chapter is about.
Finished "Strings: Working with Text"?
Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.
Was this chapter clear?
