This is a Python Program to form a string where the first character and the last character have been exchanged.
The program takes a string and swaps the first character and the last character of the string.
1. Take a string from the user and store it in a variable.
2. Pass the string as an argument to a function.
3. In the function, split the string.
4. Then add the last character to the middle part of the string which is in turn added to the first character.
5. Print the modified string.
Here is source code of the Python Program to form a string where the first character and the last character have been exchanged. The program output is also shown below.
return string[-1:] + string[1:-1] + string[:1]
Runtime Test Cases
Enter string:hello world