Ch-10 String Manipulation: "Enter The String: " "Enter The Character To Count: " "Occurs" "Times"
Ch-10 String Manipulation: "Enter The String: " "Enter The Character To Count: " "Occurs" "Times"
Question 1
Write a program to count the number of times a character occurs in the given string.
Solution
str = input("Enter the string: ")
ch = input("Enter the character to count: ");
c = str.count(ch)
print(ch, "occurs", c, "times")
Output
Enter the string: KnowledgeBoat
Enter the character to count: e
e occurs 2 times
Question 2
Write a program which replaces all vowels in the string with '*'.
Solution
Output
Question 3
Write a program which reverses a string and stores the reversed string in a new string.
Solution
str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr = ch + newStr
print(newStr)
Output
Solution
for i in range(len(lst)):
lst[i] += n
Output
Question 2
Ask the user to enter a list containing numbers between 1 and 12. Then replace all of the
entries in the list that are greater than 10 with 10.
Solution
for i in range(len(l)):
if l[i] > 10:
l[i] = 10
Output
Enter list having numbers between 1 & 12: [1, 3, 15, 8, 20]
List after removing numbers greater than 10:
[1, 3, 10, 8, 10]
Question 3a
Create the following lists using a for loop:
A list consisting of the integers 0 through 49.
Solution
l = []
for i in range(50):
l.append(i)
Output
Question 3b
Solution
l = []
Output
Question 3c
Solution
l = []
for i in range(1, 27):
l.append(chr(i + 96) * i)
print("Created List:")
print(l)
Output
Created List:
['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk',
'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo',
'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss',
'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu',
'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww',
'xxxxxxxxxxxxxxxxxxxxxxxx'
CH-12 TUPLES
Q.1 Write python that create a tuple storing first 9 term of Fibonacci series.
OUT PUT
fib = (0,1,1)
for i in range(6):
fib = fib + ( fib [ i +1] + fib [ i + 2 ] ,)
print(fib)
Q.2 Write a program to input n numbers from the user. Store these numbers in a tuple.
Print the maximum and minimum number from this tuple.
tup= ()
while True :
num = int(input("Enter a number :- "))
tup += (num,)
user = input("Do you want to quit enter yes =")
if user == "yes":
print(tup)
print("Max :-",max( tup ))
print("Min :-",min( tup ))
break
Q-3 Write a program that interactively create a nested tuple to store the marks in three
subjects for five student .
total = ()
for i in range(3):
mark = ()
mark1 = int(input("enter the marks of first subject = "))
mark2 = int(input("enter the marks of second subject = "))
mark3 = int(input("enter the marks of third subject = "))
mark = (mark1 , mark2 , mark3)
total= total + (mark,)
print("total marks = ",total)
CH-13 DICTIONARIES
Q.1 Write a program to enter names of employees and their salaries as input and store
them in a dictionary.
Answer =
dic = { }
while True :
name = input("Enter employee name :-")
sl = int(input("Enter employee salary :-"))
dic[ name] = sl
user = input("Do you want to quit then enter yes :-")
if user == "yes" :
break
print(dic)
Output :-
Enter employee name :-Path
Enter employee salary :-1000
Do you want to quit then enter yes :-no
Enter employee name :-Walla
Enter employee salary :-2000
Do you want to quit then enter yes :-no
Enter employee name :-PW
Enter employee salary :-10000
Do you want to quit then enter yes :-yes
{'Path': 1000, 'Walla': 2000, 'PW': 10000}
>>>
Q.2 Write a program to count the number of times a character appears in a given string.
Answer = tring = input("Enter String :-")
dic = { }
for i in string :
dic [ i ] = string.count( i )
print(dic)
Q. 3Write a program to convert a number entered by the user into its corresponding
number in words.
For example, if the input is 876 then the output should be 'Eight Seven Six'.
(Hint. use dictionary for keys 0-9 and their values as equivalent words.)
Answer =
num = input ("enter a number = ")
dic = { "0" : "zero" , "1":"one " , "2" : "two" , "3" : "three" , "4" : "four" , "5" : "five" , "6" :
"six" , "7" : "seven" , "8" : "eight" , "9" : "nine"}
for i in num :
print( dic[ i ], end = " ")
Output :-
enter a number = 876
eight seven six
>>>
enter a number = 123
one two three
>>>