Skip to content

About

Explore five ways to reverse strings in Python using a lightweight desktop UI, CLI, reusable functions, tests, palindrome checks, and word utilities.

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

Reverse String in Python

Python 3.9+ Tests License: MIT

A beginner-friendly collection of five ways to reverse a string in Python, available as a lightweight desktop app, command-line tool, and reusable module. The examples use clear names, type hints, docstrings, and tests so learners can compare each technique with confidence.

Desktop app

Launch the graphical interface with:

python gui.py

The app uses Python's built-in Tkinter toolkit, so there are no packages to install. It includes all five reversal techniques, reverse-word modes, palindrome checking, copy/clear buttons, and keyboard shortcuts:

  • Ctrl+Enter — transform the text
  • Escape — clear input and output

On some minimal Linux installations, Tkinter is a separate system package. For Debian or Ubuntu, install it with sudo apt install python3-tk.

Quick start

No third-party packages are required:

python reverse.py "Hello, world!"
# !dlrow ,olleH

Choose any implementation:

python reverse.py "Python" --method for
python reverse.py "Python" --method while
python reverse.py "Python" --method slice
python reverse.py "Python" --method builtin
python reverse.py "Python" --method recursive

Run python reverse.py --help for every option. With no text argument, the program prompts for input.

The five approaches

Method Idea Time Extra space Best use
for Prepend each character O(n²) O(n) Learning loops
while Read indexes right-to-left O(n²) O(n) Learning indexes
slice Use text[::-1] O(n) O(n) Most Python code
builtin Join reversed(text) O(n) O(n) Working with iterators
recursive Reverse tail, append head O(n²) O(n²) Learning recursion only

For production code, the slice version is generally the clearest. The recursive version can reach Python's recursion limit on long input.

More useful examples

python reverse.py "learn Python today" --words
# today Python learn

python reverse.py "hello world" --each-word
# olleh dlrow

python reverse.py "A man, a plan, a canal: Panama!" --palindrome
# Yes

Use it as a module too:

from reverse import is_palindrome, reverse_slice, reverse_words

print(reverse_slice("Hello"))                 # olleH
print(reverse_words("one two three"))         # three two one
print(is_palindrome("Never odd or even"))     # True

Run the tests

python -m unittest discover -s tests -v

The suite covers empty input, whitespace, Unicode text, every method, utilities, and the CLI. GitHub Actions runs it on multiple Python versions.

Unicode note: these examples reverse Unicode code points. Some visible symbols, such as emoji sequences or a letter plus a combining accent, contain multiple code points. Reversing user-perceived characters (grapheme clusters) requires a Unicode segmentation library.

Contributing

Improvements are welcome. See CONTRIBUTING.md. Please add tests for behavior changes and keep examples approachable for beginners.

License

Released under the MIT License.

About

Explore five ways to reverse strings in Python using a lightweight desktop UI, CLI, reusable functions, tests, palindrome checks, and word utilities.

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages