Python Practice › String Programs › Program 37

Swap the first and last letters of a string

EasyString Programs · 37 of 106

Write a Python program to exchange the first and last letters of a string.

Advertisement
Input
"We are learning python"
Output
ne are learning pythoW

Approach

  1. last + middle + first.

Solution

Python
text = "We are learning python"print(text[-1] + text[1:-1] + text[0])
Output
ne are learning pythoW
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs