Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Write a program to calculate number of upper case letters and number of lower case letters?Test on String: ''Tutorials POINT''

Answer»

Program is −

def string_test(s):a = { ''Lower_Case'':0 , ''Upper_Case'':0} #intiail count of lower and upperfor ch in s: #for loop   if(ch.islower()): #if-elif-else condition      a[''Lower_Case''] = a[''Lower_Case''] + 1   elif(ch.isupper()):      a[''Upper_Case''] = a [''Upper_Case''] + 1   else:      passprint(''String in testing is: '',s) #printing the statements.print(''Number of Lower Case characters in String: '',a[''Lower_Case''])print(''Number of Upper Case characters in String: '',a[''Upper_Case''])

Output −

string_test(''Tutorials POINT'')

String in testing is: Tutorials POINT

Number of Lower Case characters in String: 8

Number of Upper Case characters in String: 6

We make use of the methods .islower() and .isupper(). We initialise the count for lower and upper. Using if and else condition we calculate total number of lower and upper case characters.

2.

Write a function to give the sum of all the numbers in list?Sample list − (100, 200, 300, 400, 0, 500)Expected output − 1500

Answer»

Program for sum of all the numbers in list is −

def sum(numbers):   total = 0   for num in numbers:      total+=num   print(''Sum of the numbers: '', total)sum((100, 200, 300, 400, 0, 500))

We define a function ‘sum’ with numbers as parameter. The in for loop we store the sum of all the values of list.

3.

How will you convert a string to all uppercase?

Answer»

upper() − Converts all lowercase letters in string to uppercase.

4.

How will you remove all leading whitespace in string?

Answer»

lstrip() − Removes all leading whitespace in string.

5.

How will you convert a string to all lowercase?

Answer»

lower() − Converts all uppercase letters in string to lowercase.

6.

How will you get a space-padded string with the original string left-justified to a total of width columns?

Answer»

ljust(width[, fillchar]) − Returns a space-padded string with the original string left-justified to a total of width columns.

7.

How will you get the length of the string?

Answer»

len(string) − Returns the length of the string.