📖 Scenario: Imagine you are helping a friend who wants to learn how to reverse words in a fun way. You will create a simple program that takes a word and shows it backwards.
🎯 Goal: Build a Python program that reverses a given string using different methods step-by-step.
📋 What You'll Learn
Create a string variable with a specific word
Create a helper variable for the reversed string
Use a for loop to reverse the string manually
Print the reversed string
💡 Why This Matters
🌍 Real World
Reversing strings is useful in puzzles, coding challenges, and understanding how text can be manipulated.
💼 Career
String manipulation is a basic skill needed in programming jobs, especially when working with text data or preparing data for analysis.
Progress0 / 4 steps
1
Create the original string
Create a string variable called word and set it to the exact value "hello".
DSA Python
Hint
Use quotes to create a string. For example: word = "hello"
2
Prepare a variable for the reversed string
Create an empty string variable called reversed_word to hold the reversed result.
DSA Python
Hint
Set reversed_word to an empty string using double quotes.
3
Reverse the string using a for loop
Use a for loop with the variable char to go through each character in word. Inside the loop, add char to the front of reversed_word to build the reversed string.
DSA Python
Hint
Each time you see a character, add it before the current reversed string.
4
Print the reversed string
Print the value of reversed_word to show the reversed word.
DSA Python
Hint
Use print(reversed_word) to display the reversed string.
Practice
(1/5)
1. Which of the following methods correctly reverses a string s in Python?
easy
A. Using reversed(s) without converting to string
B. Using slicing: s[::-1]
C. Using s.reverse() method
D. Using s.append() method
Solution
Step 1: Understand string slicing
The slicing syntax s[::-1] creates a new string with characters in reverse order.
Step 2: Check other methods
s.reverse() and s.append() are list methods, not valid for strings. reversed(s) returns an iterator, not a string, so it needs conversion.
Final Answer:
Using slicing: s[::-1] -> Option B
Quick Check:
String slicing with negative step reverses string [OK]
Hint: Use s[::-1] to reverse strings quickly [OK]
Common Mistakes:
Trying to use list methods on strings
Forgetting to convert reversed() iterator to string
Using append() on strings
2. Which of the following code snippets correctly reverses the string text = 'hello' and prints it?
easy
A. print(text.append('o'))
B. print(text.reverse())
C. print(text[::-2])
D. print(''.join(reversed(text)))
Solution
Step 1: Analyze each option
text.reverse() is invalid for strings. text[::-2] reverses with step 2, skipping characters. text.append() is invalid for strings.
Step 2: Confirm correct syntax
reversed(text) returns an iterator of characters reversed. ''.join() converts it back to string. This prints 'olleh'.
Final Answer:
print(''.join(reversed(text))) -> Option D
Quick Check:
reversed() + join() prints reversed string [OK]
Hint: Use ''.join(reversed(s)) to reverse strings [OK]
Common Mistakes:
Using reverse() method on strings
Using slicing with wrong step
Trying to append to strings
3. What is the output of the following code?
def reverse_string(s):
result = ''
for char in s:
result = char + result
return result
print(reverse_string('abc'))
The function returns 'cba', which is the reverse of 'abc'.
Final Answer:
'cba' -> Option A
Quick Check:
Prepending chars reverses string [OK]
Hint: Add each char before result to reverse string [OK]
Common Mistakes:
Appending char instead of prepending
Returning original string
Confusing order of concatenation
4. The following code is intended to reverse a string but has an error. What is the error?
def reverse_str(s):
s.reverse()
return s
print(reverse_str('hello'))
medium
A. Missing return statement
B. Function returns None
C. Strings do not have reverse() method
D. Syntax error in function definition
Solution
Step 1: Check method usage on string
The reverse() method exists for lists, not strings. Calling it on a string causes AttributeError.
Step 2: Understand the error cause
Since strings are immutable, they do not support reverse(). The code will raise an error at runtime.
Final Answer:
Strings do not have reverse() method -> Option C
Quick Check:
reverse() is list method, not string method [OK]
Hint: Use slicing or reversed() for strings, not reverse() [OK]
Common Mistakes:
Trying to use reverse() on strings
Expecting reverse() to modify string in place
Ignoring immutability of strings
5. You want to reverse only the vowels in a string s = 'hello world' while keeping other characters in place. Which approach below correctly achieves this?
hard
A. Extract vowels, reverse them, then replace vowels in original positions
B. Reverse entire string using s[::-1]
C. Use reversed(s) and join all characters
D. Replace vowels with '*' and then reverse string
Solution
Step 1: Understand the problem
We want to reverse only vowels, keeping consonants and spaces fixed in their places.
Step 2: Analyze each option
Reverse entire string using s[::-1] reverses whole string, changing all characters. Use reversed(s) and join all characters reverses all characters, not just vowels. Replace vowels with '*' and then reverse string replaces vowels with '*', losing original vowels. Extract vowels, reverse them, then replace vowels in original positions extracts vowels, reverses them, then puts them back in original vowel positions, preserving other characters.
Final Answer:
Extract vowels, reverse them, then replace vowels in original positions -> Option A
Quick Check:
Reverse vowels only by extracting and replacing [OK]
Hint: Extract vowels, reverse, then put back in place [OK]
Common Mistakes:
Reversing entire string instead of vowels only
Replacing vowels with placeholders without restoring
Ignoring character positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.