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.
Launch the graphical interface with:
python gui.pyThe 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 textEscape— 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.
No third-party packages are required:
python reverse.py "Hello, world!"
# !dlrow ,olleHChoose 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 recursiveRun python reverse.py --help for every option. With no text argument, the program prompts for input.
| 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.
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
# YesUse 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")) # Truepython -m unittest discover -s tests -vThe 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.
Improvements are welcome. See CONTRIBUTING.md. Please add tests for behavior changes and keep examples approachable for beginners.
Released under the MIT License.