Python Program to Change First and Last Letter of a Word

Python Program to Change First and Last Letter of a Word

In this article, we will going to solve an interesting question based on String Manipulation. We have to change first and last letter of a word or string.

Question - Write a Python Program to Change First and Last Letter of a Word

So, without wasting any time let's solve this question. If you like the article content, then share it in your coding groups so that more people can take advantage of the content. 

Python Program to Change First and Last Letter of a Word


Logic and Explanation

Question - Write a Python Program to Change First and Last Letter of a Word

Python Program to Change First and Last Letter of a Word - Suppose we are give a string "s".

s = "Welcome"


Now we have to change first letter and last of letter of this string so for this we will first create an empty string "r". Then we will concatenate the first letter which we want to at the place of "W". Then we will iterate through string "s" from index 1 to index len(s) i.e. basically we will pass through all the letters except first and last and as we will iterate we will concatenate in  "r". Now at last we will concatenate the last letter we required to change.

I hope the logic is clear to you. If you have any doubt in this part feel free to ask in comment below. If logic is clear to you, then you can check the code.

Check More Programs Like This - Python Programs for Practice

Here's the code for Python Program to Change First and Last Letter of a Word.


s = "Welcome"

#Empty String
r = ""

#Taking input the first and last letter to be replaced
a = input("Enter first letter to be replaced:- ")
b = input("Enter last letter to be replaced:- ")

#Concatenating First Letter

#Concatenating Middle Letters
r += a
for x in range(1,len(s)):
    r = r + s[x]

#Concatenating Last Letter
r += b

print(r)


Output : - 

Enter first letter to be replaced:- d
Enter last letter to be replaced:- g
delcomeg


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

Enjoy Coding!!!


Also Read :-





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post