Reverse a String in Python Using Slicing
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This code defines a function reverse_string that takes a string as input and returns it reversed using Python's slicing feature.
The key line is input_string[::-1], which slices the entire string with a step of -1, effectively starting from the end and moving to the beginning.
- Example: The input
'Hello, World!'becomes'!dlroW ,olleH'. - This method works for any string, including letters, digits, and symbols.
- It is useful for tasks like palindrome checking or text manipulation.