store a string and print only first and last characters seperately
Answers
Answered by
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:
Similar questions
Math,
7 months ago
Accountancy,
7 months ago
English,
1 year ago
Physics,
1 year ago
English,
1 year ago