Strings in Python are immutable sequences of characters. Every string method returns a **new** string — the original is never modified.
## Case transformation
```python
text = "hello world"
print(text.upper()) # "HELLO WORLD"
print(text.lower()) # "hello world"
print(text.capitalize()) # "Hello world"
print(text.title()) # "Hello World"
print(text.swapcase()) # "HELLO WORLD"
```
## Removing whitespace
```python
s = " hello "
print(s.strip()) # "hello" — both sides
print(s.lstrip()) # "hello " — left only
print(s.rstrip()) # " hello" — right only
```
`strip()` is extremely common for cleaning user input.
## Search and check
```python
s = "Python is great"
print(s.find("is")) # 7 — index of first match, -1 if not found
print(s.index("is")) # 7 — same, but raises ValueError if not found
print(s.count("t")) # 2
print(s.startswith("Py")) # True
print(s.endswith("eat")) # True
print("123".isdigit()) # True
print("abc".isalpha()) # True
print("abc123".isalnum()) # True
```
## Replacing and splitting
```python
s = "one two three"
print(s.replace("two", "2")) # "one 2 three"
print(s.split()) # ["one", "two", "three"]
print(s.split(" ", maxsplit=1)) # ["one", "two three"]
```
`split()` without arguments splits on any whitespace and removes empty strings.
## Joining
```python
words = ["one", "two", "three"]
print(", ".join(words)) # "one, two, three"
print("-".join(words)) # "one-two-three"
print("".join(words)) # "onetwothree"
```
`join` is the opposite of `split` — always call it on the **separator**, not the list.
## Padding and alignment
```python
s = "hello"
print(s.ljust(10)) # "hello "
print(s.rjust(10)) # " hello"
print(s.center(11, "-")) # "---hello---"
print("42".zfill(5)) # "00042"
```
## Key rule
Since strings are immutable, you must **assign** the result:
```python
name = " Alice "
name = name.strip() # correct
name.strip() # does nothing — result is discarded
```
Write a function `format_name(name)` that takes a string and returns it in title case (every word starts with a capital letter). Example: `format_name("john doe")` → `"John Doe"`.
def format_name(name):
pass
print(format_name("john doe")) # John Doe
print(format_name("ALICE SMITH")) # Alice Smith
Solution
def format_name(name):
return name.title()
print(format_name("john doe")) # John Doe
print(format_name("ALICE SMITH")) # Alice Smith
Write a function `clean(s)` that removes leading/trailing whitespace and converts the string to lowercase. Example: `clean(" Hello World ")` → `"hello world"`.
def clean(s):
pass
print(clean(" Hello World ")) # hello world
print(clean(" Python ")) # python
Solution
def clean(s):
return s.strip().lower()
print(clean(" Hello World ")) # hello world
print(clean(" Python ")) # python
Write a function `count_word(text, word)` that counts how many times `word` appears in `text` (case-insensitive). Example: `count_word("Apple apple APPLE", "apple")` → `3`.
def count_word(text, word):
pass
print(count_word("Apple apple APPLE", "apple")) # 3
print(count_word("cat cat dog cat", "cat")) # 3
Solution
def count_word(text, word):
return text.lower().count(word.lower())
print(count_word("Apple apple APPLE", "apple")) # 3
print(count_word("cat cat dog cat", "cat")) # 3
Write a function `censor(text, word)` that replaces all occurrences of `word` in `text` with asterisks of the same length. Example: `censor("I love cats", "cats")` → `"I love ****"`.
def censor(text, word):
pass
print(censor("I love cats", "cats")) # I love ****
print(censor("hello world", "world")) # hello *****
Solution
def censor(text, word):
return text.replace(word, "*" * len(word))
print(censor("I love cats", "cats")) # I love ****
print(censor("hello world", "world")) # hello *****
Write a function `dashify(s)` that splits the string into words and joins them with a dash. Example: `dashify("hello world foo")` → `"hello-world-foo"`.
def dashify(s):
pass
print(dashify("hello world foo")) # hello-world-foo
print(dashify("one two three")) # one-two-three
Solution
def dashify(s):
return "-".join(s.split())
print(dashify("hello world foo")) # hello-world-foo
print(dashify("one two three")) # one-two-three
Write a function `is_url(s)` that returns `True` if the string starts with `"http://"` or `"https://"` and ends with a domain extension like `".com"`, `".org"`, or `".net"`. Otherwise return `False`.
Write a function `greet(name, age)` that returns a greeting using an f-string: `"Hello, {name}! You are {age} years old."`. Example: `greet("Alice", 25)` → `"Hello, Alice! You are 25 years old."`.
def greet(name, age):
pass
print(greet("Alice", 25)) # Hello, Alice! You are 25 years old.
print(greet("Bob", 30)) # Hello, Bob! You are 30 years old.
Solution
def greet(name, age):
return f"Hello, {name}! You are {age} years old."
print(greet("Alice", 25)) # Hello, Alice! You are 25 years old.
print(greet("Bob", 30)) # Hello, Bob! You are 30 years old.
Write a function `format_price(amount)` that formats a number as a price with 2 decimal places and a `$` prefix. Example: `format_price(9.5)` → `"$9.50"`, `format_price(1000)` → `"$1,000.00"`.
Write a function `safe_find(text, sub)` that returns the index of the first occurrence of `sub` in `text`, or `-1` if not found. Do NOT use `.index()` (it raises an error). Example: `safe_find("hello", "ll")` → `2`, `safe_find("hello", "x")` → `-1`.
Write a function `progress_bar(done, total, width=20)` that returns a text progress bar. Fill with `"#"` for completed portion, `"."` for remaining. Example: `progress_bar(3, 10, 10)` → `"[###.......] 30%"`.
We use necessary cookies to run the site. With your permission, we can also save your site preferences and use analytics and advertising cookies to understand usage and support the project.
Open tools in tabs.Exercises, IDE tools, and trainers stay available as site tabs.
Switch without losing context.Move between explanations, code, and utilities while keeping your place.
Use the sidebar as your map.The left panels hold navigation, settings, files, libraries, and tool controls.
PythonJavaScriptSQLite
One IDE, three practical modes
Python in the browser.Run small scripts, try libraries, and practice API requests without installing anything.
JavaScript for quick experiments.Test browser-friendly code and compare ideas next to your learning materials.
SQLite for data practice.Open the database explorer to inspect tables, write queries, and learn SQL workflows locally.
TopicIDE
Work side by side with split tabs
Keep instructions visible.Open an exercise or reference page beside the IDE instead of jumping back and forth.
Compare tools while you learn.Place regex checks, explanations, and code experiments next to each other when the task needs it.
Close the split when you are done.The workspace returns to a single focused tab, and your open site tabs remain available.
Code Typing Trainer
Or plain text
This trainer is designed for a physical keyboard.Open this section on a laptop or desktop with a wide screen. Touch typing practice will not work correctly on a phone.
Speed: 0 chars/min
0 words/min
Best speed (60s): 0 chars/min
0 words/min
Errors: 0
Total time: 0.0 s
To practice touch typing, avoid looking at your physical keyboard.