Computer Science
Given a string s = "12345". Can you write an expression that gives sum of all the digits shown inside the string s i.e., the program should be able to produce the result as 15 (1+2+3+4+5).
[Hint. Use indexes and convert to integer]
Python Data Handling
34 Likes
Answer
print(int(s[0]) + int(s[1]) + int(s[2]) + int(s[3]) + int(s[4]))
Answered By
23 Likes
Related Questions
Find the errors(s)
name = "HariT" print (name) name[2] = 'R' print (name)
Predict the output if e is given input as 'True':
a = True b = 0 < 5 print (a == b) print (a is b) c = str (a) d = str (b) print (c == d) print (c is d) e = input ("Enter :") print (c == e) print (c is e)
Following expression does not report an error even if it has a sub-expression with 'divide by zero' problem:
3 or 10/0
What changes can you make to above expression so that Python reports this error?
What is the output produced by following code?
a, b = bool(0), bool(0.0) c, d = str(0), str(0.0) print (len(a), len(b)) print (len(c), len(d))