Strings are words or sentences in code. Changing them helps us clean, format, or prepare text for different uses.
Common string transformations in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
string.upper() string.lower() string.strip() string.replace(old, new) string.split(separator) separator.join(list_of_strings) string.startswith(prefix) string.endswith(suffix)
These methods do not change the original string but return a new one.
Strings are like beads on a string; these methods help rearrange or clean them.
Examples
Python
text = "Hello World" print(text.upper())
Python
text = " hello " print(text.strip())
Python
text = "apple,banana,orange" fruits = text.split(",") print(fruits)
Python
words = ["I", "love", "Python"] sentence = " ".join(words) print(sentence)
Sample Program
This program shows many common string changes: uppercase, trimming spaces, replacing characters, splitting, joining, and checking start/end.
Python
text = " Hello, Python World! " # Make uppercase upper_text = text.upper() # Remove spaces clean_text = text.strip() # Replace comma with dash replaced_text = clean_text.replace(",", " -") # Split into words words = replaced_text.split() # Join words with underscore joined_text = "_".join(words) # Check start and end starts_with_hello = clean_text.startswith("Hello") ends_with_world = clean_text.endswith("World!") print("Uppercase:", upper_text) print("Cleaned:", clean_text) print("Replaced:", replaced_text) print("Words:", words) print("Joined:", joined_text) print("Starts with 'Hello':", starts_with_hello) print("Ends with 'World!':", ends_with_world)
Important Notes
Remember strings are not changed directly; methods return new strings.
Use strip() to clean user input before processing.
split() without arguments splits on any whitespace.
Summary
Use string methods to change or check text easily.
Common methods: upper(), lower(), strip(), replace(), split(), join(), startswith(), endswith().
These help prepare text for display, comparison, or storage.
Practice
1. Which Python string method converts all characters in a string to uppercase?
text = "hello world"easy
Solution
Step 1: Understand the purpose of each method
upper() converts all letters to uppercase, lower() to lowercase, strip() removes spaces, replace() changes parts of the string.Step 2: Identify the method that changes all letters to uppercase
upper() is the method that does this.Final Answer:
text.upper() -> Option AQuick Check:
upper() = text.upper() [OK]
Hint: Uppercase all letters with upper() method [OK]
Common Mistakes:
- Confusing upper() with lower()
- Using strip() to change case
- Trying replace() without arguments
2. Which of the following is the correct syntax to remove whitespace from both ends of the string
text?easy
Solution
Step 1: Recall Python string methods for trimming spaces
Python uses strip() to remove whitespace from both ends of a string.Step 2: Check the options for correct method name
trim(), remove(), and cut() are not valid Python string methods.Final Answer:
text.strip() -> Option AQuick Check:
strip() = text.strip() [OK]
Hint: Use strip() to remove spaces from start and end [OK]
Common Mistakes:
- Using trim() which is not a Python method
- Trying remove() or cut() which don't exist
- Confusing strip() with replace()
3. What is the output of the following code?
text = "apple,banana,cherry"
result = text.split(",")
print(result)medium
Solution
Step 1: Understand split() method with comma separator
split(",") breaks the string at each comma, creating a list of parts.Step 2: Apply split to the string
Splitting "apple,banana,cherry" by comma gives ['apple', 'banana', 'cherry'].Final Answer:
['apple', 'banana', 'cherry'] -> Option DQuick Check:
split(",") = list of words [OK]
Hint: split(',') breaks string into list by commas [OK]
Common Mistakes:
- Expecting a string instead of list
- Using split() without argument
- Confusing split() with join()
4. The following code is intended to replace all spaces with dashes in the string
text. What is the error?text = "hello world"
text.replace(" ", "-")
print(text)medium
Solution
Step 1: Understand string immutability in Python
Strings cannot be changed in place; methods like replace() return a new string.Step 2: Check code behavior
text.replace(" ", "-") returns new string but original text remains unchanged because result is not assigned.Final Answer:
Output is 'hello world' because replace() does not change original string -> Option CQuick Check:
replace() returns new string, assign it [OK]
Hint: Assign replace() result to variable to update string [OK]
Common Mistakes:
- Not assigning replace() result
- Expecting replace() to modify string in place
- Confusing syntax errors with logic errors
5. You have a list of words:
words = [' apple', 'Banana ', ' CHERRY ']. Which code correctly creates a new list with all words trimmed and in lowercase?hard
Solution
Step 1: Understand the order of strip() and lower()
strip() removes spaces, lower() converts to lowercase. Order doesn't matter here since they don't interfere.Step 2: Analyze each option
A concatenates strip() result with lower() result, doubling content. B calls lower() then references .strip without (), yielding method objects. C chains strip().lower() correctly. D calls strip() on list, causing AttributeError.Final Answer:
[word.strip().lower() for word in words] -> Option BQuick Check:
List comprehension with strip() and lower() [OK]
Hint: Use list comprehension with strip() then lower() [OK]
Common Mistakes:
- Concatenating strings instead of chaining methods
- Calling strip() on list
- Forgetting parentheses on strip()
