Python

Python

Python

 View Only
  • 1.  Reverse a string in Python

    Posted Tue September 28, 2021 10:18 AM

    I was reading this article on how to reverse a string in python, I understood the concept. But when I am trying to implement the same I am having doubts.

    The Problem -

    I can not use ".join(reversed(string)) or string[::-1] methods here so it's a bit tricky.

    My code looks something like this:

    def reverse(text):
        while len(text) > 0:
            print text[(len(text)) - 1],
            del(text[(len(text)) - 1]


    I
    use the , to print out every single letter in text on the same line!

    I get invalid syntax on del(text[(len(text)) - 1]

    Any suggestions?

    Thank you!

     



    ------------------------------
    Vikash Jain
    ------------------------------


  • 2.  RE: Reverse a string in Python

    Posted Tue September 28, 2021 11:08 AM

    Hi.

    This community is mainly for Python 3 (specifically on z/OS), as it looks like you're using Python 2 which is no longer supported, so we'd recommend using the newer version of Python. But for your question - you can find multiple answers here - https://stackoverflow.com/a/27843760/15424126 (specifically the function "reverse_a_string_slowly"). The answer provided defines a function which reverses the string manually.

    Thanks,

    Steven



    ------------------------------
    Steven Pitman
    ------------------------------