Write a python program that:Prompt the user for a stringExtract all the digits from the string.If there are digitsSum the collected digits together.Printout:The original stringThe digitsThe sum of the digitsIf there are no digitsPrint the original stringA message “Has no Digits”
Question
Write a python program that:Prompt the user for a stringExtract all the digits from the string.If there are digitsSum the collected digits together.Printout:The original stringThe digitsThe sum of the digitsIf there are no digitsPrint the original stringA message “Has no Digits”
Solution
Sure, here is a Python program that fulfills your requirements:
# Prompt the user for a string
user_string = input("Please enter a string: ")
# Extract all the digits from the string
digits = [int(char) for char in user_string if char.isdigit()]
# Check if there are digits
if digits:
# Sum the collected digits together
sum_of_digits = sum(digits)
# Print out the original string, the digits, and the sum of the digits
print("The original string: ", user_string)
print("The digits: ", digits)
print("The sum of the digits: ", sum_of_digits)
else:
# Print the original string and a message “Has no Digits”
print("The original string: ", user_string)
print("Has no Digits")
This program first prompts the user for a string. It then extracts all the digits from the string and checks if there are any. If there are, it sums them up and prints out the original string, the digits, and their sum. If there are no digits, it prints out the original string and a message saying "Has no Digits".
Similar Questions
/*Write a program to check if a given number is COMPLETE NUMBER or NOTNote:A Number is said to be COMPLETE NUMBER if the individual digits of the number are all even if the individual digits of the number are not even then print NOT COMPLETE NUMBER
Now build a program to take string (combination of characters and digits) as input from the user and remove all the digits from the given string.Print the result as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Hello123WorldString·after·removing·all·digits:·HelloWorldTest Case 2:Expected Output:str:·1234567890String·after·removing·all·digits:·Test Case 3:Expected Output:str:·PythonProgrammingString·after·removing·all·digits:·PythonProgramming
Python program that accepts a string and calculate the number of digits and letters and space
Write a Shell script to print given numbers sum of all digits.
Write a program to check if string contain only Numbers
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.