
Python
Python strings come with powerful built-in methods. These methods don't modify the original string (strings are immutable), but return new strings.
Convert strings to uppercase or lowercase:
text = "Hello World"
print(text.upper()) # HELLO WORLD
print(text.lower()) # hello world
print(text.capitalize()) # Hello world
print(text.title()) # Hello WorldRemove leading and trailing whitespace:
messy = " Python \n"
print(messy.strip()) # Python (both sides)
print(messy.lstrip()) # Python \n (left side)
print(messy.rstrip()) # Python (right side)
print(messy.strip()) # Python
# Remove specific characters
url = "...example.com..."
print(url.strip(".")) # example.comReplace substrings with new text:
sentence = "I like apples. I eat apples daily."
print(sentence.replace("apples", "oranges"))
# I like oranges. I eat oranges daily.
# Replace only first N occurrences
print(sentence.replace("apples", "oranges", 1))
# I like oranges. I eat apples daily.Split strings into lists or join lists into strings:
# Splitting
sentence = "Python is awesome"
words = sentence.split() # Default: split by whitespace
print(words) # ['Python', 'is', 'awesome']
csv_data = "apple,banana,orange"
fruits = csv_data.split(",")
print(fruits) # ['apple', 'banana', 'orange']
# Joining
word_list = ["Hello", "beautiful", "world"]
message = " ".join(word_list)
print(message) # Hello beautiful world
# Join with different separator
numbers = ["1", "2", "3"]
print("-".join(numbers)) # 1-2-3| Method | Purpose | Example |
|---|---|---|
| `upper()` | Convert to uppercase | `"hello".upper()` → `'HELLO'` |
| `lower()` | Convert to lowercase | `"HELLO".lower()` → `'hello'` |
| `strip()` | Remove whitespace | `" hello ".strip()` → `'hello'` |
| `replace()` | Replace text | `"hi".replace("h", "H")` → `'Hi'` |
| `split()` | Split into list | `"a,b".split(",")` → `['a', 'b']` |
| `join()` | Join from list | `",".join(['a', 'b'])` → `'a,b'` |
| `find()` | Find position | `"hello".find("l")` → `2` |
| `startswith()` | Check beginning | `"hello".startswith("he")` → `True` |
| `endswith()` | Check ending | `"hello".endswith("lo")` → `True` |
# Clean and process user data
user_input = " JOHN DOE "
processed = user_input.strip().lower().replace(" ", "_")
print(processed) # john_doe
# This works because methods return new strings
# that can be used immediately (method chaining)| Concept | Remember |
|---|---|
| Methods don't modify | String methods return new strings; originals stay the same |
| Method chaining | Call multiple methods in sequence: `text.strip().upper()` |
| Split/join patterns | Use `split()` to break apart, `join()` to reassemble |
| Case conversion | `upper()`, `lower()`, `capitalize()`, `title()` cover most needs |
| Whitespace removal | `strip()`, `lstrip()`, `rstrip()` for different sides |
Ready to format strings dynamically? Learn about f-strings and formatting.
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward