Computer Science
Write a Python program to change a given string to a new string where the first and last characters have been exchanged.
Python
Python String Manipulation
2 Likes
Answer
input_str = input("Enter the string: ")
first_char = input_str[0]
last_char = input_str[-1]
middle_chars = input_str[1:-1]
new_str = last_char + middle_chars + first_char
print("Original string:", input_str)
print("New string after swapping first and last characters:", new_str)
Output
Enter the string: python
Original string: python
New string after swapping first and last characters: nythop
Answered By
1 Like