Python program: Reverse a string
Reverse text in three ways.
The code
text = "Brisbane" print(text[::-1]) # slicing: the usual way print("".join(reversed(text))) # reversed + join result = "" for ch in text: # a loop: each character goes in front result = ch + result print(result)
Output
enabsirB enabsirB enabsirB
How it works
A slice with step −1 walks backwards. The loop version shows what is happening underneath.
Python features used
Learn the ideas behind it in the free lesson Strings.