5(iii)
Write a Python program to do the following tasks.
- (a)Input a sentence and store it to a string variable.
- (b)Print the length of the string, its first character, and its last character.
- (c)Use a loop to traverse through the string and print the total number of digits present.
Show SolutionHide Solution↓
sentence = input("Enter a sentence: ")
l = len(sentence)
fc = sentence[0]
lc = sentence[-1]
print("Length of the string:", l)
print("First character:", fc)
print("Last character:", lc)
count = 0
for ch in sentence:
if ch.isdigit():
count += 1
print("Total number of digits:", count)
Enter a sentence: Hello123 World45
Length of the string: 16
First character: H
Last character: 5
Total number of digits: 5