String Reversal with Lambda

Write a lambda function that takes a string and returns the reversed string.

Example 1:

Input: 'hello'
Output: 'olleh'

Example 2:

Input: 'world'
Output: 'dlrow'

Use a lambda function and string slicing.

# Define the lambda function
reverse_str = lambda s: s[::-1]

# Test the function
print(reverse_str('hello')) # Output: 'olleh'
print(reverse_str('world')) # Output: 'dlrow'

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!