AiTechWorlds
AiTechWorlds
Every time your phone autocorrects your message, a search engine finds relevant results, or a website validates your email address — text manipulation is happening. Billions of strings are processed every second across the internet.
In Python, strings aren't just passive containers for text. They come loaded with built-in methods that let you search, transform, split, join, and analyze text with just a dot and a method name.
Before diving into methods, there's one fundamental rule: strings cannot be changed in place.
name = "alice"
name.upper() # This does NOT change name
print(name) # Still prints: alice
name = name.upper() # THIS works — you reassign the result
print(name) # Now prints: ALICE
Every string method returns a new string. The original string is untouched. You have to capture the result to use it.
These are the methods you'll use almost daily:
text = "hello, World! this IS Python."
print(text.upper()) # HELLO, WORLD! THIS IS PYTHON.
print(text.lower()) # hello, world! this is python.
print(text.title()) # Hello, World! This Is Python.
print(text.capitalize()) # Hello, world! this is python.
print(text.swapcase()) # HELLO, wORLD! THIS is pYTHON.
When to use each:
.lower() — Comparing user input (so "YES", "yes", "Yes" all match).upper() — Display headers, labels.title() — Names, article titles.capitalize() — First word only (like a sentence)Users often accidentally add spaces. These methods clean up input:
user_input = " alice@example.com "
print(repr(user_input)) # ' alice@example.com '
print(repr(user_input.strip())) # 'alice@example.com'
print(repr(user_input.lstrip())) # 'alice@example.com '
print(repr(user_input.rstrip())) # ' alice@example.com'
repr() is used here so you can see the quotes and spaces. In real code you'd just use:
email = input("Enter your email: ").strip().lower()
This single line removes surrounding spaces AND makes it lowercase. Combining methods like this is called method chaining.
sentence = "The quick brown fox jumps over the lazy dog"
# Find position of first occurrence
print(sentence.find("fox")) # 16 (position 16 from start)
print(sentence.find("cat")) # -1 (not found — returns -1)
# Count occurrences
print(sentence.count("the")) # 1 (case-sensitive!)
print(sentence.lower().count("the")) # 2
# Check if contains
print("fox" in sentence) # True
print("cat" in sentence) # False
# Check start/end
print(sentence.startswith("The")) # True
print(sentence.endswith("dog")) # True
Tip: Always use
infor simple "does it contain?" checks — it's more readable than.find().
text = "I love JavaScript. JavaScript is everywhere."
# Replace all occurrences
new_text = text.replace("JavaScript", "Python")
print(new_text)
# I love Python. Python is everywhere.
# Replace only first occurrence
first_only = text.replace("JavaScript", "Python", 1)
print(first_only)
# I love Python. JavaScript is everywhere.
Splitting a string into a list:
csv_line = "Alice,25,Engineer,London"
parts = csv_line.split(",")
print(parts) # ['Alice', '25', 'Engineer', 'London']
print(parts[0]) # Alice
print(parts[2]) # Engineer
# Split by whitespace (default — splits on any whitespace)
words = " hello world ".split()
print(words) # ['hello', 'world'] (extra spaces ignored!)
Joining a list back into a string:
words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence) # Python is awesome
tags = ["python", "programming", "beginner"]
tag_string = ", ".join(tags)
print(tag_string) # python, programming, beginner
.join() is the opposite of .split(). The string you call it on becomes the separator.
You've seen f-strings already, but let's go deeper:
name = "Alice"
score = 95.678
items = 3
# Basic insertion
print(f"Hello, {name}!")
# Expressions inside f-strings
print(f"Score: {score:.1f}") # 1 decimal place: 95.7
print(f"Score: {score:.0f}") # 0 decimal places: 96
print(f"Score: {score:10.2f}") # Width 10, 2 decimals: " 95.68"
# Padding and alignment
print(f"{'Left':<10}|{'Right':>10}|{'Center':^10}")
# Left | Right| Center |
# Calculations
print(f"Items: {items}, Total: ${items * 9.99:.2f}")
| Format Code | Meaning | Example |
|---|---|---|
:.2f | 2 decimal places | 3.14159 → 3.14 |
:10 | Width of 10 characters | "hi" → "hi " |
:<10 | Left-aligned, width 10 | "hi" → "hi " |
:>10 | Right-aligned, width 10 | "hi" → " hi" |
:^10 | Centered, width 10 | "hi" → " hi " |
:, | Thousands separator | 1000000 → 1,000,000 |
| Method | What It Does | Returns |
|---|---|---|
.upper() | All uppercase | New string |
.lower() | All lowercase | New string |
.title() | Title Case | New string |
.strip() | Remove surrounding whitespace | New string |
.split(sep) | Split into list | List |
sep.join(list) | Join list into string | New string |
.replace(old, new) | Replace occurrences | New string |
.find(sub) | First position (-1 if not found) | Integer |
.count(sub) | Count occurrences | Integer |
.startswith(s) | Starts with this? | Boolean |
.endswith(s) | Ends with this? | Boolean |
.isdigit() | All digits? | Boolean |
.isalpha() | All letters? | Boolean |
.isspace() | All whitespace? | Boolean |
def analyze_text(text):
"""Analyze a piece of text and return statistics."""
if not text.strip():
return None
words = text.split()
sentences = text.count('.') + text.count('!') + text.count('?')
sentences = max(sentences, 1) # At least 1
word_freq = {}
for word in text.lower().split():
clean_word = word.strip('.,!?";:')
if clean_word:
word_freq[clean_word] = word_freq.get(clean_word, 0) + 1
top_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:5]
return {
"characters": len(text),
"characters_no_spaces": len(text.replace(" ", "")),
"words": len(words),
"sentences": sentences,
"avg_word_length": sum(len(w) for w in words) / len(words),
"avg_words_per_sentence": len(words) / sentences,
"top_words": top_words,
}
sample = """Python is a powerful programming language. Python is easy to learn
and Python is widely used in data science, web development, and automation.
Many developers choose Python for their first programming language."""
stats = analyze_text(sample)
if stats:
print("=== Text Analysis ===")
print(f"Characters: {stats['characters']}")
print(f"Characters (no sp.): {stats['characters_no_spaces']}")
print(f"Words: {stats['words']}")
print(f"Sentences: {stats['sentences']}")
print(f"Avg word length: {stats['avg_word_length']:.1f} chars")
print(f"Avg words/sentence: {stats['avg_words_per_sentence']:.1f}")
print(f"\nTop 5 words:")
for word, count in stats['top_words']:
bar = "█" * count
print(f" {word:<15} {bar} ({count})")
Output:
=== Text Analysis ===
Characters: 224
Characters (no sp.): 186
Words: 38
Sentences: 3
Avg word length: 6.3 chars
Avg words/sentence: 12.7
Top 5 words:
python ███ (3)
is ███ (3)
and ██ (2)
programming ██ (2)
language ██ (2)
String methods are the foundation of text processing in Python. Once you're comfortable with them, tasks like parsing log files, processing user input, cleaning data, and building simple search features become natural.
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises