Reverse a String

Category: Python Basics
Tags: Strings

Write a program that takes a string as input and returns the string in reverse order.

Example:

Input: "Hello, World!" 

Output: "!dlroW ,olleH"

In Python, strings are sequences of characters, which means they can be indexed. You can use negative indexing and slicing techniques to reverse the order of the string.

def reverse_string(s):
return s[::-1]

print(reverse_string("Hello, World!")) # Outputs: "!dlroW ,olleH"

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!