Bird
Raised Fist0
Pythonprogramming~20 mins

Common string transformations in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
String Transformation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of string strip and replace
What is the output of this Python code?
Python
text = "  Hello, World!  "
result = text.strip().replace(",", "")
print(result)
A" Hello World! "
B"Hello World!"
C"Hello, World!"
D"Hello,World!"
Attempts:
2 left
๐Ÿ’ก Hint
strip() removes spaces at start and end; replace() removes commas.
โ“ Predict Output
intermediate
2:00remaining
Result of string split and join
What is the output of this code?
Python
sentence = "Python is fun"
words = sentence.split()
result = "-".join(words)
print(result)
A"Python-is-fun"
B"Python is fun"
C"Python-isfun"
D"Python-is-fun-"
Attempts:
2 left
๐Ÿ’ก Hint
split() breaks string into words; join() connects them with dashes.
โ“ Predict Output
advanced
2:00remaining
Output of string formatting with f-string
What does this code print?
Python
name = "Alice"
age = 30
print(f"Name: {name.upper()}, Age: {age + 5}")
A"Name: ALICE, Age: 30"
B"Name: alice, Age: 35"
C"Name: Alice, Age: 30"
D"Name: ALICE, Age: 35"
Attempts:
2 left
๐Ÿ’ก Hint
upper() makes name uppercase; age + 5 adds 5 to age.
โ“ Predict Output
advanced
2:00remaining
Result of chained string methods
What is printed by this code?
Python
text = "  python Programming  "
result = text.strip().capitalize().replace("programming", "Coding")
print(result)
A"Python Programming"
B"Python coding"
C"Python Coding"
D"python Coding"
Attempts:
2 left
๐Ÿ’ก Hint
strip() removes spaces, capitalize() makes first letter uppercase, replace() swaps words.
โ“ Predict Output
expert
3:00remaining
Output of complex string slicing and methods
What is the output of this code?
Python
s = "DataScience"
result = s[1:9:2].lower() + s[-3:].upper()
print(result)
A"aaceNCE"
B"atcienceCE"
C"atcEENCE"
D"AaceNCE"
Attempts:
2 left
๐Ÿ’ก Hint
Slice with step 2 from index 1 to 9: indices 1('a'),3('a'),5('c'),7('e') -> 'aace'.lower(), then add last 3 uppercase.

Practice

(1/5)
1. Which Python string method converts all characters in a string to uppercase?
text = "hello world"
easy
A. text.upper()
B. text.lower()
C. text.strip()
D. text.replace()

Solution

  1. 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.
  2. Step 2: Identify the method that changes all letters to uppercase

    upper() is the method that does this.
  3. Final Answer:

    text.upper() -> Option A
  4. Quick 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
A. text.strip()
B. text.trim()
C. text.remove()
D. text.cut()

Solution

  1. Step 1: Recall Python string methods for trimming spaces

    Python uses strip() to remove whitespace from both ends of a string.
  2. Step 2: Check the options for correct method name

    trim(), remove(), and cut() are not valid Python string methods.
  3. Final Answer:

    text.strip() -> Option A
  4. Quick 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
A. apple,banana,cherry
B. ['apple,banana,cherry']
C. apple banana cherry
D. ['apple', 'banana', 'cherry']

Solution

  1. Step 1: Understand split() method with comma separator

    split(",") breaks the string at each comma, creating a list of parts.
  2. Step 2: Apply split to the string

    Splitting "apple,banana,cherry" by comma gives ['apple', 'banana', 'cherry'].
  3. Final Answer:

    ['apple', 'banana', 'cherry'] -> Option D
  4. Quick 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
A. No error, output is 'hello-world'
B. RuntimeError because replace() needs assignment
C. Output is 'hello world' because replace() does not change original string
D. SyntaxError due to wrong replace() usage

Solution

  1. Step 1: Understand string immutability in Python

    Strings cannot be changed in place; methods like replace() return a new string.
  2. Step 2: Check code behavior

    text.replace(" ", "-") returns new string but original text remains unchanged because result is not assigned.
  3. Final Answer:

    Output is 'hello world' because replace() does not change original string -> Option C
  4. Quick 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
A. [word.strip() + word.lower() for word in words]
B. [word.strip().lower() for word in words]
C. [word.lower().strip for word in words]
D. [word.lower() for word in words.strip()]

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    [word.strip().lower() for word in words] -> Option B
  4. Quick 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()