Computer Science, asked by Christo20045, 9 months ago

Write a program to accept a string. Extract the last character and form a new string after adding the extracted character at the first and last places in the given string. Print both the strings.
Example :
Input: "fat"
Output: "tfatt"
(Where t is the last character in the string fat) ?

Answers

Answered by Equestriadash
4

string = input("Enter a string: ")

chr = string[-1]

newstring = chr + string + chr

print(newstring)

Characters can be extracted if one knows the index values of each character in a string.

Since it's impossible to identify the number of characters a user would input, it's best to use negative indexing.

When using negative indexing, -1 always represents the last character in the string, as negative indexing starts backwards.

Once that's extracted, you concatenate it before and after the string.

Attachments:
Similar questions