Basics of Python / String Manipulation – Revision 3
1. Evaluate the following expression:
a) 26//4//7
b) 2**2**3
c) (20 + 10)*15/5
d) 2+9*((3*12)-8)/10
e) 10 + 6 * 2 ** 2 != 9//4 -3 and 29>= 29/9
f) 5 % 10 + 10 < 50 and 29 <= 29
g) (0 < 6) or (not (10 == 6) and (10<0))
h) 5 % 10 + 10 < 50 and 29//7 <= 29%7
2. Guess the correct output of the following code?
a)
str1 = "Examination"
print(str1[1:4], str1[:5], str1[4:], str1[::-1])
print(str1[::-2],str1[-5:-7],str1[-5:-1])
b) Str1 = "EXAM2020"
Str2 = ""
I=0
while I<len(Str1):
if Str1[I]>="A" and Str1[I]<="M":
Str2=Str2+Str1[I+1]
elif Str1[I]>="0" and Str1[I]<="9":
Str2=Str2+ (Str1[I-1])
else:
Str2=Str2+"*"
I=I+1
print(Str2)
3. Write Python programs for the following:
1. Read a string from the user and count words beginning with A/a and ending with T/t.
2. Read a string from the user and display words that does not contain alphabet E/e.
3. Read a string from the user and count alphanumeric characters.
4. Read a string from the user and extract all the digits from the string and display it a
single number and also find the sum of all the digits present in the string.
1. Evaluate the following expression:
a) 26//4//7
b) 2**2**3
c) (20 + 10)*15/5
d) 2+9*((3*12)-8)/10
e) 10 + 6 * 2 ** 2 != 9//4 -3 and 29>= 29/9
f) 5 % 10 + 10 < 50 and 29 <= 29
g) (0 < 6) or (not (10 == 6) and (10<0))
h) 5 % 10 + 10 < 50 and 29//7 <= 29%7
2. Guess the correct output of the following code?
a)
str1 = "Examination"
print(str1[1:4], str1[:5], str1[4:], str1[::-1])
print(str1[::-2],str1[-5:-7],str1[-5:-1])
b) Str1 = "EXAM2020"
Str2 = ""
I=0
while I<len(Str1):
if Str1[I]>="A" and Str1[I]<="M":
Str2=Str2+Str1[I+1]
elif Str1[I]>="0" and Str1[I]<="9":
Str2=Str2+ (Str1[I-1])
else:
Str2=Str2+"*"
I=I+1
print(Str2)
3. Write Python programs for the following:
1. Read a string from the user and count words beginning with A/a and ending with T/t.
2. Read a string from the user and display words that does not contain alphabet E/e.
3. Read a string from the user and count alphanumeric characters.
4. Read a string from the user and extract all the digits from the string and display it a
single number and also find the sum of all the digits present in the string.