[go: up one dir, main page]

0% found this document useful (0 votes)
106 views8 pages

Ch-10 String Manipulation: "Enter The String: " "Enter The Character To Count: " "Occurs" "Times"

The document provides solutions to string and list manipulation problems in Python. It includes 3 questions on string manipulation - counting characters in a string, replacing all vowels with asterisks, and reversing a string. It also includes 3 questions on list manipulation - incrementing list elements by a number, replacing values in a list greater than 10 with 10, and using for loops to generate lists of integers, squares, and increasing character strings. Finally, it discusses tuples and dictionaries with examples of nested tuples for student marks and dictionaries for employee names and salaries.

Uploaded by

Parthiv Mistry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views8 pages

Ch-10 String Manipulation: "Enter The String: " "Enter The Character To Count: " "Occurs" "Times"

The document provides solutions to string and list manipulation problems in Python. It includes 3 questions on string manipulation - counting characters in a string, replacing all vowels with asterisks, and reversing a string. It also includes 3 questions on list manipulation - incrementing list elements by a number, replacing values in a list greater than 10 with 10, and using for loops to generate lists of integers, squares, and increasing character strings. Finally, it discusses tuples and dictionaries with examples of nested tuples for student marks and dictionaries for employee names and salaries.

Uploaded by

Parthiv Mistry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ch-10 STRING MANIPULATION

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

str = input("Enter the string: ")


newStr = ""
for ch in str :
lch = ch.lower()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
newStr += '*'
else :
newStr += ch
print(newStr)

Output

Enter the string: Computer Studies


C*mp*t*r St*d**s

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

Enter the string: computer studies


seiduts retupmoc
CH-11 LIST MANIPULATION
Question 1

Write a program to increment the elements of a list with a number.

Solution

lst = eval(input("Enter a list: "))


print("Existing list is:", lst)

n = int(input("Enter a number: "))

for i in range(len(lst)):
lst[i] += n

print("List after increment:", lst)

Output

Enter a list: [1, 2, 3, 4, 5]


Existing list is: [1, 2, 3, 4, 5]
Enter a number: 10
List after increment: [11, 12, 13, 14, 15]

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

l = eval(input("Enter list having numbers between 1 & 12: "))

for i in range(len(l)):
if l[i] > 10:
l[i] = 10

print("List after removing numbers greater than 10:")


print(l)

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)

print("List with integers from 0 to 49:")


print(l)

Output

List with integers from 0 to 49:


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

Question 3b

Create the following lists using a for loop:


A list containing the squares of the integers 1 through 50.

Solution

l = []

for i in range(1, 51):


l.append(i * i)

print("List with square of integers from 1 to 50:")


print(l)

Output

List with square of integers from 1 to 50:


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441,
484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369,
1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500]

Question 3c

Create the following lists using a for loop:


The list ['a','bb','ccc','dddd', . . . ] that ends with 26 copies of the letter z.

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
>>>

You might also like