Computer Science, asked by SomyaShirotriya, 1 year ago

store a string and print only first and last characters seperately

Answers

Answered by Equestriadash
5

string = input("Enter a string: ")

fch = string[0]

lch = string[-1]

print(fch, "is the first character.")

print(lch, "is the last character.")

The first character can be extracted by using positive indexing, that traverses through a string forwards.

The last character can be extracted by using negative indexing, that traverses through a string backwards.

Indexing is also most commonly required when performing string slicing.

Output:

\tt Enter\ a\ string: \sf Python\ Programming\\\\\tt P\ is\ the\ first\ character.\\g\ is\ the\ last\ character.

Similar questions