[go: up one dir, main page]

0% found this document useful (0 votes)
84 views13 pages

Ass-Strings and Dictionaries

This document contains 27 questions related to string handling and operations in Python. The questions cover basic string operations like slicing, indexing, concatenation and manipulation. They also cover string methods like find, count, split, upper, lower, capitalize etc. Finally, the document contains questions related to dictionaries like creation, updating, deleting, iterating, copying and built-in methods.
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)
84 views13 pages

Ass-Strings and Dictionaries

This document contains 27 questions related to string handling and operations in Python. The questions cover basic string operations like slicing, indexing, concatenation and manipulation. They also cover string methods like find, count, split, upper, lower, capitalize etc. Finally, the document contains questions related to dictionaries like creation, updating, deleting, iterating, copying and built-in methods.
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/ 13

ASSIGNMENT-4

CLASS : XI COMPUTER SCIENCE


DATE :
WORKSHEET – STRING HANDLING

1 What will be the output of following code-


str="hello"
str[:2]

2 What will be the output of following code-


str='Hello'
res=''
for i in range(len(str)):
res=res+ “i”
print(res)

3 What will be the output of following code-


s='Hi!'
s1='Hello'
s2=s[:2]+s1[len(s1)-2:]
print(s2)

4 What will be the output of following code-


'ba'+'na'*2

5 What will be the output of following code-


s='Welcome to python4csip.com'
print(s.find('come'))
s.count('o')

1
6 What will be the output of following program:
s='Hello'
for i in s:
print(i,end='#')

7 What will be the output of following program:


for i in 'hardik':
print(i.upper())

8 What will be the output of following program:


s='Hello'
for i in s:
print('Welcome')

9 What will be the output of following program:


str='virat'
for i in str:
print(str.upper())

10 What will be the output of following program:

a='hello'
b='virat'
for i in range(len(a)):
print(a[i],b[i])

11 What will be the output of following program:


a='hello'
b='virat'
for i in range(len(a)):
print(a[i].upper(),b[i])

12 What will be the output of following program:


print("xyyzxyzxzxyy".count('xyy', 2, 11))
13 What will be the output of following program:
print(“hello”+1+2+3)

14 What will be the output of following program:


str1 = "PYTHON4CSIP.COM"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])

2
15 What will be the output of following
program: str = "my name is kunfu
pandya";
print (str.capitalize())

16 What will be the output of following


program: str1 = 'Hello'
str2 ='World!'
print('str1 + str2 = ', str1 +
str2) print('str1 * 3 =', str1
* 3)

17 What will be the output of following


program: count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')

18 What will be the output of following program:


s="python
4csip" n =
len(s)
m=''
for i in range(0, n):
if (s[i] >= 'a' and s[i]
<= 'm'): m = m +
s[i].upper()
elif (s[i] >= 'n' and s[i]
<= 'z'): m = m + s[i-
1]
elif (s[i].isupper()):
m=m+
s[i].lower() else:
m = m + '#'
print(m)

19 What will be the output of following


program: a = "Mahender Singh Dhoni"
a = a.split()
b = a[0][0]+". "+a[1][0]+". "+a[2]
print (b)

3
20 What will be the output of following
program: s='Mahender, Singh, Dhoni'
s1=s.sp
lit() for
i in s1:
print(i)

21 What will be the output of following program:


"Welcome to Python4csip.com".split()

22 What will be the output of following program:


str ='Hello Python'
print (str)
print (str[0])
print (str[2:8])
print (str[3:])
print (str * 3)
print (str + "String")

23 What will be the output of the program?


line = "PYTHON IS EASY TO LEARN"
L = line.split('a')
for i in L:
print(i, end=' ')

24 What will be the output of following program:


s='mahender, singh, dhoni'
s1=s.split()
for i in s1:
if(i>'n'):
print(i.upper())
else:
print(i)

4
25 What will be the output of following program:
my_string = 'PYTHON4CSIP'
for i in range(len(my_string)):
print (my_string)
my_string = '#'

26 What will be the output of following program:


str="Python4csip.com"
for i in range(len(str)):
if(str[i].isalpha()):
print(str[i-1],end='')
if(str[i].isdigit()):
print(str[i],end='')

27 What will be the output of following program:


str="AB145CVD124N"
for i in range(len(str)):
if(str[i].isalpha()):
print(str[i-1],end='')
if(str[i].isdigit()):
print('#',end='')

28 What will be the output of following


program: str="PYTHON4CSIP"
for i in
range(len(str)):
if(str[i].isdigit()):
print(str[i],end='')
if(str[i]=='N'or str[i]=='Y'):
print('#',end='')

5
6
7
DICTIONARIES
1. What will be the output of following code
a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")

2. What will be the output of following code


a={i: 'Hi!' + str(i) for i in range(5)}
a

3. What will be the output of following code


D = dict()
for i in range (3):
for j in range(2):
D[i] = j
8
print(D)

4. What will be the output of following code


a={i: i*i for i in range(6)}
a

5.What will be the output of following code


a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])

6. What will be the output of following program:


dictionary = {1:'1', 2:'2', 3:'3'}
del dictionary[1]
dictionary[1] = '10'
del dictionary[2]
print(len(dictionary))

7.Predict the Output:


dict1 = {"name": "Mike", "salary": 8000}
temp = dict1.pop("age")
print(temp)

8. What will be the output of following program:


dict1 = {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)

9 What will be the output of following program:


dict={"Virat":1,"Rohit":2}
dict.update({"Rahul":2})
print(dict)

10. What will be the output of following program:


a=dict()
a[1]

11.What will be the output of following program:


a={}
a.fromkeys([1,2,3],"check")

12. What will be the output of following program:


a={}
a['a']=1
a['b']=[2,3,4]
print(a)

13. What will be the output of following program:


a = {}
a[1] = 1
9
a['1'] = 2
a[1.0]=4
count = 0
for i in a:
count += a[i]
print(count)

14. What will be the output of following program:


test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test)

15 .What will be the output of following program:


test = {1:'A', 2:'B', 3:'C'}
test = {}
print(len(test))

16 .What will be the output of following program:


a={1:"A",2:"B",3:"C"}
del a

17. What will be the output of following program:


a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")

18 What will be the output of following program:


a={1:5,2:3,3:4}
a.pop(3)
print(a)

19.What will be the output of following program:


a={1:5,2:3,3:4}
print(a.pop(4,9))
print()

20. What will be the output of following program:


a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D")
print(a)

21 .What will be the output of following program:


a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

22 .What will be the output of following program:


10
a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

23.What will be the output of the program?


a = {}
a[1] = 1
a['1'] = 2
a[1]=a[1]+1
count = 0
for i in a:
count += a[i]
print(count)

24. What will be the output of following program:


box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
print(len([crates]))

25 What will be the output of following program:


dict = {'c': 97, 'a': 96, 'b': 98}
for _ in sorted(dict):
print (dict[_])

26 .What will be the output of following program:


rec = {"Name" : "Python", "Age":"20"}
r = rec.copy()
print(id(r) == id(rec))

27 What will be the output of following program:


rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id1 = id(rec)
del rec
rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id2 = id(rec)
print(id1 == id2)

28. What will be the output of following program:


my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)] = 12
sum = 0
11
for k in my_dict:
sum += my_dict[k]
print (sum)
print(my_dict)

29.What will be the output of following program:


my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)

30. What will be the output of following program:


arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum += arr[k]
print (sum)

31. What will be the output of following program:


a = {'a':1,'b':2,'c':3}
print (a['a','b'])

32 .What will be the output of following program:


a = {(1,2):1,(2,3):2}
print(a[1,2])

33. What will be the output of following program:


a={ 1:'a', 2:'b', 3:'c'}
for i,j in a.items():
print(i,j,end="#")

34. What will be the output of following program:


aList = [1,2,3,5,6,1,5,6,1]
fDict = {}
for i in aList:
fDict[i] = aList.count(i)
print (fDict)

35. What will be the output of following program:


plant={}
plant[1]='Rohit'
plant[2]='Sharma'
plant['name']='Ishant'
12
plant[4]='Sharma'
print (plant[2])
print (plant['name'])
print (plant[1])
print (plant)

36 .What will be the output of following program:


dict = {'Name': 'Rohit', 'Age': 30}
print (dict.items())

37. Write a Python program to iterate over dictionaries using for loops
d = {"blue" : 1, "green" : 2, "yellow" : 3}
for key,value in d.item:
print(key, value)

13

You might also like