Lecture

String Reversal - Solution

Explore three methods to reverse a string.


Method 1
def solution(s): reversed_str = "" # Create an empty string for char in s: # Iterate over the string reversed_str = char + reversed_str # Prepend each character to the new string return reversed_str # Return the reversed string

Using the code above, you can return a reversed version of the given string s.


Example Usage

Input/Output Example
input_string = "hello" result = solution(input_string) print(result) # Output: "olleh"

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help