Changing the First and Last Letter of a Word in Python
·Dec 26, 2022·
2 min read
Play this article
In this tutorial, you will learn how to write a Python program to change the first and last letter of a given word. Let's get started with the code. If you find this article helpful, please consider sharing it with your coding communities and social media groups so that others can benefit from the content as well.
To change the first and last letter of a word, we will follow these steps:
- Define the word that we want to modify as a string variable. In this example, we are using the word "Welcome":
s = "Welcome"
- Create an empty string called
rto store the modified version of the word.
r = ""
- Prompt the user to enter the first and last letters that they want to replace.
a = input("Enter first letter to be replaced:- ") b = input("Enter last letter to be replaced:- ")
- Concatenate the first letter (entered by the user) to the
r += a
- Iterate through the characters in the
sstring, starting at index 1 (to skip the first letter) and ending at the second-to-last index (to skip the last letter). As we iterate through the string, we will concatenate each character to the
for x in range(1,len(s)): r = r + s[x]
- Concatenate the last letter (entered by the user) to the
r += b
- Print out the modified version of the word stored in the
Here is the complete code for this program:
s = "Welcome" r = "" a = input("Enter first letter to be replaced:- ") b = input("Enter last letter to be replaced:- ") r += a for x in range(1,len(s)): r = r + s[x] r += b print(r)
For example, if the user enters "f" for the first letter and "m" for the last letter, the output will be:
I hope this code and explanation are helpful in understanding how to change the first and last letter of a word in Python. Thank you for reading!