• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Reverse a string in Python

Posted Feb 8, 2024 — Updated Apr 6, 2024

The simplest way is to use Python double colon slicing with a negative step value:

my_string = "abc123"
reversed_string = my_string[::-1]
print(reversed_string)
"321cba"
Python

You can also use the builtin reversed method, which will “Return a reverse iterator over the values of the given sequence” according to its official help(). Note that this returns an iterator, not a string, so you’ll have to do a bit more work to create the reversed string:

my_string = "abc123"
reverse_iterator = reversed(my_string)
# <reversed at 0x1051bead0>

new_string = ""
for char in reverse_iterator:
  new_string += char
  
print(new_string)
"321cba"
Python

You could also build a list from the reversed object and then .join it like so:

my_string = "abc123"
reverse_iterator = reversed(my_string)
# <reversed at 0x1051bead0>

"".join(list(reversed_iterator))
# "321cba"
Python

Both of these methods work equally well on lists.

Filed Under: Python

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy