Exercise 20 

Write a program in Python language, to exchange the first and last characters of a given string.

Solution

# define example of string s
s = "my-courses.net"
# getting the len of string s
n = len(s)
#getting the first and last caracter of string s
first = s[0]
last = s[n-1]
# extracing the sub string by ignoring the first and last character
s1 = s[1:n-1]
# make up the new string by exchanging the first and last character
s2 = last + s1 + first
print(s2)
Younes Derfoufi
One thought on “Solution Exercise 20”

Leave a Reply