Robotics & Artificial Intelligence

Write a Python program to do the following tasks.

  1. Input a sentence and store it to a string variable.
  2. Print the length of the string, its first character, and its last character.
  3. Use a loop to traverse through the string and print the total number of digits present.

Python String Manipulation

1 Like

Answer

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)

Output

Enter a sentence: Hello123 World45
Length of the string: 16
First character: H
Last character: 5
Total number of digits: 5

Answered By

1 Like


Related Questions