Write a Python Program to Change a Given String to a New String Where The First And Last Chars Have Been Exchanged

In this article you will learn a Python program that takes a string as input and interchanges the first and last character of the given string.  finally prints the output after  interchanging the first and last characters to the standard output.

You need to understand a few things to solve this Python program.

  1. To access the first  character of a string you use index zero [0]
  2. To access the last character of string you use index minus one [-1]
  3. To get the middle characters of the string by ignoring the first character and last character,  use the index with the string slicing [1:-1]


After understanding these three string access methods. let's try to solve this while using Python program.

 

Write a Python Program to Change a Given String to a New String Where The First And Last Chars Have Been Exchanged

 



Write a Python Program to Change a Given String to a New String Where The First And Last Chars Have Been Exchanged


Take a string variable name as data and use The input function call as the user  to enter a string.  

Then take another variable named new_string,  assign the last character of a given string using [-1]  index.

Middle of the string by ignoring the first and last character  using [1:-1]

Now at the first character of a string to the new_string  and complete the final string.

data = input("Enter a String : ")
new_string = data[-1] + data[1:-1] + data[0]





Use the print function call to print the string new_string with a user friendly message. This will interchange the first and last characters of a string  and print it to standard output.

print("String after Interchanging The First And Last Chars is :", new_string)




Complete Python Program
===================================

data = input("Enter a String : ")
new_string = data[-1] + data[1:-1] + data[0]
print("String after Interchanging The First And Last Chars is :", new_string)




Output 1
===============

Enter a String : hello
String after Interchanging The First And Last Chars is : oleeh




Output 2
=================

Enter a String : code with tj
String after Interchanging The First And Last Chars is : jode with tc



Output 3
==================

Enter a String : project
String after Interchanging The First And Last Chars is : trojecp




Output 4
==================

Enter a String : process
String after Interchanging The First And Last Chars is : srocesp




Conclusion
============

The above Python program uses the string slicing with the string indexing methods to interchange the first and last characters of a string to get a new string out of it.

Execute the above Python program by  providing the random  string as input to the Python program and not down the results.

Comment it down below if you have any  where is regarding the above python program. Also comment below if you have any other method to solve this Python program

Post a Comment

0 Comments