Computer Science
Write a program that does the following :
- takes two inputs : the first, an integer and the second, a string
- from the input string extract all the digits, in the order they occurred, from the string.
- if no digits occur, set the extracted digits to 0
- add the integer input and the digits extracted from the string together as integers
- print a string of the form :
"integerinput + stringdigits = sum"
For example :
For inputs 12, 'abc123' → '12 + 123 = 135'
For inputs 20, 'a5b6c7' → '20 + 567 =587'
For inputs 100, 'hi mom' → '100 + 0 = 100'
Python
Python String Manipulation
6 Likes
Answer
num = int(input("Enter an integer: "))
str = input("Enter the string: ")
digitsStr = ''
digitsNum = 0;
for ch in str :
if ch.isdigit() :
digitsStr += ch
if digitsStr :
digitsNum = int(digitsStr)
print(num, "+", digitsNum, "=", (num + digitsNum))
Output
Enter an integer: 12
Enter the string: abc123
12 + 123 = 135
=====================================
Enter an integer: 20
Enter the string: a5b6c7
20 + 567 = 587
=====================================
Enter an integer: 100
Enter the string: hi mom
100 + 0 = 100
Answered By
1 Like
Related Questions
Write a program that should prompt the user to type some sentence(s) followed by "enter". It should then print the original sentence(s) and the following statistics relating to the sentence(s) :
- Number of words
- Number of characters (including white-space and punctuation)
- Percentage of characters that are alphanumeric
Hints
- Assume any consecutive sequence of non-blank characters is a word.
Write a program to convert a given number into equivalent Roman number (store its value as a string). You can use following guidelines to develop solution for it:
- From the given number, pick successive digits, using %10 and /10 to gather the digits from right to left.
- The rules for Roman Numerals involve using four pairs of symbols for ones and five, tens and fifties, hundreds and five hundreds. An additional symbol for thousands covers all the relevant bases.
- When a number is followed by the same or smaller number, it means addition. "II" is two 1's = 2. "VI" is 5 + 1 = 6.
- When one number is followed by a larger number, it means subtraction. "IX" is 1 before 10 = 9. "IIX isn't allowed, this would be "VIII". For numbers from 1 to 9, the symbols are "I" and "V", and the coding works like this. "I" , "II", "III", "IV", "V", "VI", "VII", "VIII", "IX".
- The same rules work for numbers from 10 to 90, using "X" and "L". For numbers from 100 to 900, using the symbols "C" and "D". For numbers between 1000 and 4000, using "M".
Here are some examples. 1994 = MCMXCIV, 1956 = MCMLVI, 3888= MMMDCCCLXXXVIII
Write a Python program as per specifications given below:
- Repeatedly prompt for a sentence (string) or for 'q' to quit.
- Upon input of a sentence s, print the string produced from s by converting each lower case letter to upper case and each upper case letter to lower case.
- All other characters are left unchanged.
For example,
Please enter a sentence, or 'q' to quit : This is the Bomb!
tHIS IS THE bOMB!
Please enter a sentence, or 'q ' to quit : What's up Doc ???
wHAT'S UP dOC ???
Please enter a sentence, or 'q' to quit : qWrite a program that takes two strings from the user and displays the smaller string in single line and the larger string as per this format :
1st letter last letter 2nd letter 2nd last letter 3rd letter 3rd last letter
For example,
if the two strings entered are Python and PANDA then the output of the program should be :PANDA P n y o t h