Sequence Slicing

Reverse String
Reverse String in Python
def reverse(text):
  return text[::-1]

This approach uses Python's negative indexes and sequence slices to iterate over the string in reverse order, returning a reversed copy.

index from left ⟹






0
👇🏾
1
👇🏾
2
👇🏾
3
👇🏾
4
👇🏾
5
👇🏾
P y t h o n
👆🏾
-6
👆🏾
-5
👆🏾
-4
👆🏾
-3
👆🏾
-2
👆🏾
-1





⟸ index from right

Slices use [<start> : <stop> : <step>] syntax. The space before the first : indicates which index to start iterating from (inclusive), the space before the second : indicates which index to stop before (exclusive), and the final space after the second : indicates the direction of iteration and size of the 'step'. A positive step moves left --> right and a negative step moves right --> left. If start/stop indexes are omitted, Python assumes 'start of string' and 'end of string'. Omitting the step defaults to a step of +1, but any size step can be used. Slices return a copy of the original object. This same syntax works on strings, bytearray, lists, tuples, and ranges, which are all sequence types.

Reverse slicing has O(n) time complexity - the amount of time/work scales directly with the length of the string being iterated through and reversed. And since slicing returns copy, the space for the copy also scales with the size of the input.

Using a slice on a string is roughly equivalent to looping over the string from the right-hand side, appending each codepoint to a new string. However, the code below takes O(n + n) best case and O(n**2) worst case due to the operations needed for string concatenation.

def reverse(text):
  output = ''
  for index in range(-1, -(len(text)+1), -1):
    output += text[index] 
  return output
26th Mar 2025 · Found it useful?

Other Approaches to Reverse String in Python

Other ways our community solved this exercise
def reverse(text):
    output = ''
    for codepoint in text:
        output = codepoint + output
    return output
Iteration and Concatenation

Iterate through the codepoints and concatenate them to a new string.

def reverse(text):
    new_word = ""
    for index in range(len(text) - 1, -1, -1):
        new_word += text[index]
    return new_word
Backward iteration with Range

Use a negative step with range() to iterate backward and append to a new string.

def reverse(text):
    output = list(text)
    start, end = 0, len(text) - 1
    while start < end:
        output[start], output[end] = output[end], output[start]
        start += 1
        end -= 1
    return "".join(output)
Make a list and use join()

Create a list from the string and use join to make a new string.

def reverse(text):
	output = list(text)
	output.reverse()

	return ''.join(output)
Use the built-in list.reverse() function.

Create a list of codepoints, use list.reverse() to reverse in place, and join() to make a new string.

def reverse(text):
  return (''.join(reversed(text)))
Use the built-in reversed() function.

Use reversed() and unpack it with join() to make a new string.

given, output = bytearray(text.encode("utf-8")), bytearray(len(given))
    index, LENGTH_MASK  = 0, 0xE0  # 0b11110000 or 224
    while index < len(given):
        seq_len = not(given[index] >> 7) or (given[index] & LENGTH_MASK).bit_count()
        location = index + seq_len +1
        output[-location:-index or None] = given[index:index + seq_len]
        index += seq_len
    return output.decode("utf-8")
Additional approaches that are further afield

Additional interesting approaches.