Reverse a String with Slicing

Easy ⏱ 8 min 82% acceptance ★★★★☆ 4.2
Write a function reverse_string(s) that returns the reverse of the string s using slicing only — no loops, no reversed(), no .reverse(). This is the fastest, most Pythonic way to flip a string.

Examples

Example 1
Input
s = 'hello'
Output
'olleh'
Explanation

Extended slicing with a step of -1 walks the string backwards.

Example 2
Input
s = 'DataVix'
Output
'xiVataD'
Explanation

Works for any string, including mixed case.

Constraints

  • 0 <= len(s) <= 10^5
  • Must use slice notation, not a loop or built-in reverse.

Topics

StringsSlicing

Companies

AmazonTCSInfosys

Hints

Hint 1

`s[::-1]` reads the string from end to start.

Hint 2

The step argument of a slice can be negative.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.