I decided to step out of my comfort zone (Java) and try out Python. I wrote a very simple reverser.py and want it to be reviewed.

This python script basically takes a string and reverses it.

This python script can be used as a command line script as well as a separate
import sys

def reverse(message):
    This function takes a string and reverses it, so as to somewhat obfuscate

    :param message: The unencrypted text message to be reversed.
    text = ''
    for character in message:
        text = character + text
    return text

if __name__ == '__main__':
    numberOfArgs = len(sys.argv)

    message = ''
    if numberOfArgs == 1:  # i.e. the program name only
        message = input('Please enter the text to be encrypted : ')
    elif numberOfArgs == 2:
        message = sys.argv[1]
        print('Incorrect number of arguments : %d\n' % numberOfArgs)
        print('Usage: python3 reverseCipher [unencrypted string]')
        exit(2)  # '2' stands for command-line syntax errors in unix


Examples in the terminal:

$ python3 reverser.py

Please enter the text to be encrypted : I understand that text manipulation in python is quite easy.
.ysae etiuq si nohtyp ni noitalupinam txet taht dnatsrednu I

$ python3 reverser.py "I am just starting out with python"

nohtyp htiw tuo gnitrats tsuj ma I

It would be great if I would be advised about:

  1. Code style and idioms
  2. The required mindset (for I have one for Java)
  3. Anything that can help me learn the language

2 Answers 2


This is going to be a short answer, but did you know Python has such a feature in-built?

text = "I'm a stringy!"

Above would replace your reverse method and is the most Pythonic way to solve this.

This is called extended slicing and has been added since Python 2.3. Slicing is a very easy to use method when handling array-like data.

The following illustrates how slicing and extended slicing can be used, executed in the interactive Python IDE:

In [1]: text = "I'm a stringy!"

In [2]: print(text)
I'm a stringy!

In [3]: print(text[:-1])
I'm a stringy

In [4]: print(text[::-1])
!ygnirts a m'I

In [5]: print(text[:4:-1])

In [6]: print((text:4:-1] + text[:3:5])[::-1])
I stringy!

In [7]: print(text[::-1][::-1][::-1])
!ygnirts a m'I

As picture.

  • 1
    \$\begingroup\$ also useful is ''.join(reversed(text)), but in this case reverse-stepping is great. \$\endgroup\$
    – Adam Smith
    Commented Nov 25, 2015 at 21:09

I guess you might have heard of PEP8, which is the style guideline for Python. Contrary to a lot of other programming languages, this style guideline is actually used a lot, and is the main standard for coding in Python.

Your code looks clean, and I like that you've commented using docstrings moth the module/file and the function. Mast does have a point that you can accomplish the same using message[::-1], but part of going into a new language is learning what the language can offer in standard libraries.

Do look into using slice'ing, which is extremely powerful and useful. A challenge for you is to resolve Project Euler #13: Large sum using Python and sliceing. I think you'll get some pleasant surprises related to string vs number handling, and handling of large numbers as well.

Two other comments related to style issues:

  • Use snake_case for variable and function naming – That is use number_of_arguments instead of numberOfArgs
  • Look into using format for string formatting – The % is scheduled for depreceation, and you should rather use print('Incorrect number of arguments: {}'.format(number_of_arguments)). For more on this see Format specification mini language, on that page there is also useful examples and syntax for how to pretty print using format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.