Logic Building
# Take string input
s = input("Enter a string: ")
# Swap first and last
if len(s) >= 2:
result = s[-1] + s[1:-1] + s[0]
print(f"After swap: {result}")
else:
print("String too short")Output
Enter a string: Hello After swap: oellH
Reconstruct string with swapped characters.
Key Concepts:
- s[-1] is last character
- s[1:-1] is middle part
- s[0] is first character