CBSE Class 12 Computer Science Question 84 of 120

Review of Python Basics — Question 36

Back to all questions
36
Question

Question 31

Write a Python program to change a given string to a new string where the first and last characters have been exchanged.

Solution
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
Answer