{"id":53466,"date":"2024-06-06T16:08:04","date_gmt":"2024-06-06T10:38:04","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=53466"},"modified":"2026-07-17T09:38:35","modified_gmt":"2026-07-17T04:08:35","slug":"python-reverse-string-with-examples","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/python-reverse-string-with-examples\/","title":{"rendered":"Python Reverse String: 6 Methods with Code and Speed Comparison"},"content":{"rendered":"\n<p>In the versatile world of programming, Python reverse string stands as a fundamental yet crucial skill that every coder should master.<\/p>\n\n\n\n<p>Understanding how to reverse a string in Python not only sharpens your problem-solving skills but also opens up a myriad of possibilities for manipulating data and implementing algorithms.<\/p>\n\n\n\n<p>This article delves into seven effective methods for reversing strings in Python, providing you with a comprehensive guide to tackle this common problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li>Python strings don&#8217;t have a built in <code>.reverse()<\/code> method, so you need a workaround.<\/li>\n\n\n\n<li>The fastest and most common method is slicing: <code>text[::-1]<\/code>.<\/li>\n\n\n\n<li>Other reliable options include <code>reversed()<\/code> with <code>join()<\/code>, a for loop, <code>join()<\/code> with <code>list.reverse()<\/code>, recursion, and a stack based approach.<\/li>\n\n\n\n<li>All 6 methods run in O(n) time for most practical purposes, but they differ in readability, space usage, and interview suitability.<\/li>\n\n\n\n<li>If you need to reverse the order of words instead of characters, that&#8217;s a different problem with a different solution.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Python Doesn&#8217;t Have a Built-In String Reverse<\/strong><\/h2>\n\n\n\n<p>If you&#8217;ve searched for a <code>.reverse()<\/code> method for strings and come up empty, you&#8217;re not alone. <a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> strings are immutable, which means they can&#8217;t be changed in place the way lists can.<\/p>\n\n\n\n<p>That&#8217;s exactly why list has a <a href=\"https:\/\/realpython.com\/ref\/builtin-functions\/reversed\/\" target=\"_blank\" rel=\"noreferrer noopener\"><code>.reverse()<\/code> method<\/a> and string doesn&#8217;t. You&#8217;ll need one of the six approaches below instead, and picking the right one depends on whether you&#8217;re writing production code or preparing for a coding interview.<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <br \/><br \/>\n  Python strings skip the .reverse() method entirely because they&#8217;re immutable. Mutable sequences like lists get in-place methods such as .reverse() and .append(), but strings always return a new object instead of modifying themselves.\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6 Ways to Reverse a String in Python<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"657\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM-1200x657.webp\" alt=\"6 Ways to Reverse a String in Python\" class=\"wp-image-123466\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM-1200x657.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM-300x164.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM-768x421.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM-1536x841.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM-150x82.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_56_35-AM.webp 1694w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Slicing with <code>[::-1]<\/code><\/strong><\/h3>\n\n\n\n<p>This is the method most <a href=\"https:\/\/www.guvi.in\/blog\/python-developer-roles-and-responsibilities\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python developers<\/a> reach for first. It&#8217;s short, readable, and doesn&#8217;t need a loop or import.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"GUVI\"\nreversed_text = text&#91;::-1]\nprint(reversed_text)  # IVUG<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>reversed()<\/code> with <code>join()<\/code><\/strong><\/h3>\n\n\n\n<p><code>reversed()<\/code> returns an iterator, not a string, so you pair it with <code>\"\".join()<\/code> to rebuild the string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"GUVI\"\nreversed_text = \"\".join(reversed(text))\nprint(reversed_text)  # IVUG<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. For Loop<\/strong><\/h3>\n\n\n\n<p>Useful when you want to see exactly how the reversal happens, character by character.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def reverse_with_loop(text):\n    result = \"\"\n    for char in text:\n        result = char + result\n    return result\n\nprint(reverse_with_loop(\"GUVI\"))  # IVUG<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. <code>join()<\/code> with <code>list.reverse()<\/code><\/strong><\/h3>\n\n\n\n<p>Here you convert the string to a list, reverse the list in place, then join it back.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def reverse_with_join(text):\n    chars = list(text)\n    chars.reverse()\n    return \"\".join(chars)\n\nprint(reverse_with_join(\"GUVI\"))  # IVUG<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Recursion<\/strong><\/h3>\n\n\n\n<p>This method breaks the string down until it hits an empty string, then rebuilds it in reverse as the calls return.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def reverse_recursive(text):\n    if len(text) == 0:\n        return text\n    return reverse_recursive(text&#91;1:]) + text&#91;0]\n\nprint(reverse_recursive(\"GUVI\"))  # IVUG<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Stack (LIFO)<\/strong><\/h3>\n\n\n\n<p>Push every character onto a stack, then pop them off. Since a stack is last in, first out, the popped order is automatically reversed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def reverse_with_stack(text):\n    stack = list(text)\n    result = \"\"\n    while stack:\n        result += stack.pop()\n    return result\n\nprint(reverse_with_stack(\"GUVI\"))  # IVUG<\/code><\/pre>\n\n\n\n<p><em><strong>If you want to build a stronger foundation before moving to advanced string and data structure problems, HCL GUVI&#8217;s <a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=python-reverse-string-with-examples\" target=\"_blank\" rel=\"noreferrer noopener\">Python Course<\/a> covers this with hands on projects and placement support.<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method Comparison In Python Reverse String: Time, Space, and Readability<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM-1200x675.webp\" alt=\"Method Comparison In Python Reverse String: Time, Space, and Readability\" class=\"wp-image-123465\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM-1200x675.webp 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM-300x169.webp 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM-768x432.webp 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM-1536x864.webp 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM-150x84.webp 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/07\/ChatGPT-Image-Jul-15-2026-07_59_35-AM.webp 1672w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Time Complexity<\/th><th>Space Complexity<\/th><th>Readability<\/th><th>When to Use<\/th><\/tr><\/thead><tbody><tr><td>Slicing <code>[::-1]<\/code><\/td><td>O(n)<\/td><td>O(n)<\/td><td>High<\/td><td>Everyday scripts, quick fixes<\/td><\/tr><tr><td><code>reversed()<\/code> + <code>join()<\/code><\/td><td>O(n)<\/td><td>O(n)<\/td><td>High<\/td><td>Pythonic, iterator based style<\/td><\/tr><tr><td>For loop<\/td><td>O(n\u00b2)*<\/td><td>O(n)<\/td><td>Medium<\/td><td>Learning how reversal works internally<\/td><\/tr><tr><td><code>join()<\/code> + <code>list.reverse()<\/code><\/td><td>O(n)<\/td><td>O(n)<\/td><td>Medium<\/td><td>When you already have a list of characters<\/td><\/tr><tr><td>Recursion<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Low for beginners<\/td><td>Practicing recursive thinking<\/td><\/tr><tr><td>Stack<\/td><td>O(n)<\/td><td>O(n)<\/td><td>Medium<\/td><td>Demonstrating LIFO logic in interviews<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Method Comparison: Time, Space, and Readability<\/figcaption><\/figure>\n\n\n\n<p>*The for loop version is O(n\u00b2) in the worst case because each concatenation (<code>char + result<\/code>) creates a brand new string, since strings are immutable. For short strings this rarely matters, but avoid it for large text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reverse String Without Slicing: For Interview Coding Rounds<\/strong><\/h2>\n\n\n\n<p>Some interviewers specifically ask you to reverse a string without slicing, to check if you understand pointer based logic. Here&#8217;s the two-pointer swap approach, done on a list since strings can&#8217;t be modified in place.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def reverse_without_slicing(text):\n    chars = list(text)\n    left, right = 0, len(chars) - 1\n    while left &lt; right:\n        chars&#91;left], chars&#91;right] = chars&#91;right], chars&#91;left]\n        left += 1\n        right -= 1\n    return \"\".join(chars)\n\nprint(reverse_without_slicing(\"GUVI\"))  # IVUG<\/code><\/pre>\n\n\n\n<p>You start with pointers at both ends of the list, swap the characters, then move the pointers toward the middle. This is the same logic used to check for palindromes, so it&#8217;s worth practicing before interviews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reverse Words in a String (Not Characters): A Different Problem<\/strong><\/h2>\n\n\n\n<p>This one trips up a lot of beginners. Reversing a string flips the character order. Reversing the words in a sentence keeps each word intact but flips their order.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sentence = \"Learn Python with GUVI\"\nreversed_words = \" \".join(sentence.split()&#91;::-1])\nprint(reversed_words)  # GUVI with Python Learn<\/code><\/pre>\n\n\n\n<p>Here, <code>.split()<\/code> breaks the sentence into a list of words, <code>[::-1]<\/code> reverses that list, and <code>\" \".join()<\/code> puts it back together with spaces. If you&#8217;re asked this in an interview, clarify which version they mean before you start coding.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Mistakes to Avoid<\/strong><\/h2>\n\n\n\n<ol>\n<li><strong>Assuming strings have a <code>.reverse()<\/code> method.<\/strong> Only lists have it. Calling <code>.reverse()<\/code> on a string raises an <code>AttributeError<\/code>. Use slicing or <code>reversed()<\/code> instead.<\/li>\n\n\n\n<li><strong>Using string concatenation inside a loop for long text.<\/strong> Since strings are immutable, each concatenation builds a new string. For large inputs, this gets slow. Prefer <code>join()<\/code> or slicing.<\/li>\n\n\n\n<li><strong>Mixing up &#8220;reverse a string&#8221; with &#8220;reverse the words in a string.&#8221;<\/strong> These are two different problems with two different solutions, and interviewers often ask both.<\/li>\n\n\n\n<li><strong>Forgetting the base case in recursion.<\/strong> Skipping it for an empty string causes infinite recursion and a <code>RecursionError<\/code>.<\/li>\n<\/ol>\n\n\n\n<p><em><strong>Also read: <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-recursion-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mastering Recursion in Python: A Comprehensive Guide<\/a><\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Reverse String Python Interview Questions List<\/strong><\/h2>\n\n\n\n<ul>\n<li>How do you reverse a string in Python without using slicing?<\/li>\n\n\n\n<li>What&#8217;s the time complexity difference between slicing and a for loop?<\/li>\n\n\n\n<li>How would you reverse only the words in a sentence, not the characters?<\/li>\n\n\n\n<li>Can you use <code>.reverse()<\/code> on a string? Why not?<\/li>\n\n\n\n<li>How do you check if a string is a palindrome using reversal?<\/li>\n\n\n\n<li>What&#8217;s the difference between <code>reversed()<\/code> and <code>[::-1]<\/code>?<\/li>\n\n\n\n<li>How would you reverse a string using recursion, and what&#8217;s its space complexity?<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center\"><em><strong>Kickstart your Programming journey by enrolling in<\/strong><\/em><strong><em> HCL GUVI\u2019s<a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=python-reverse-string-with-examples\" target=\"_blank\" rel=\"noreferrer noopener\"> Python Course<\/a><\/em><\/strong><em><strong> where you will master technologies like multiple exceptions, classes, OOPS concepts, dictionaries, and many more, and build real-life projects.<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Concluding Thoughts&#8230;<\/strong><\/h2>\n\n\n\n<p>Reversing a string in Python comes down to picking the right tool for the job. Slicing and <code>reversed()<\/code> handle most everyday cases with clean, readable code. Loops, recursion, and stacks matter more for interviews, where they show you understand what&#8217;s happening under the hood.<\/p>\n\n\n\n<p>Once you&#8217;re comfortable with these six methods, try the word reversal problem and the no-slicing version too. Together, they cover almost every version of this question you&#8217;ll run into, whether it&#8217;s a coding assignment or a technical interview round.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1717396797584\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can you use reverse () on a string?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, the <code>reverse()<\/code> The method cannot be used on a string in Python, as it is specifically designed for lists.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1717396800397\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is the fastest way to reverse a string in Python?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Slicing (text[::-1]) is generally the fastest and most readable option for typical string lengths.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1717396801519\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you reverse a string without using slicing?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use a two-pointer swap on a list of characters, or use <code>reversed()<\/code> with <code>join()<\/code>. This is common in interview rounds that restrict slicing.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1717396802875\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you reverse the words in a sentence instead of the characters?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Split the sentence into words, reverse that list, then join it back with spaces: <code>\" \".join(sentence.split()[::-1])<\/code>.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1784081794331\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>What is the time complexity of reversing a string in Python?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Most methods, including slicing, <code>reversed()<\/code>, and the stack based approach, run in O(n) time, where n is the length of the string.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1784081805423\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>Does reversing a string change the original string?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. Strings in Python are immutable, so every method here returns a new string instead of modifying the original.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In the versatile world of programming, Python reverse string stands as a fundamental yet crucial skill that every coder should master. Understanding how to reverse a string in Python not only sharpens your problem-solving skills but also opens up a myriad of possibilities for manipulating data and implementing algorithms. This article delves into seven effective [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":123463,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717],"tags":[],"views":"16175","authorinfo":{"name":"Lukesh S","url":"https:\/\/www.guvi.in\/blog\/author\/lukesh\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/06\/Feature-image-6-300x116.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/53466"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=53466"}],"version-history":[{"count":33,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/53466\/revisions"}],"predecessor-version":[{"id":123901,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/53466\/revisions\/123901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/123463"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=53466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=53466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=53466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}