Type A
1 . str = input("Enter the string: ")
length = len(str)
for a in range(0, length, 2):
print(str[a:a+2])
5 . Strings are sequence of characters where each character has a unique index. This implies that strings are iterable like lists but unlike lists they are
immutable so they cannot be modified at runtime. Therefore, strings can't be considered as character lists. For example,
str = 'cat'
# The below statement
# is INVALID as strings
# are immutable
str[0] = 'b'
# Considering character lists
strList = ['c', 'a', 't']
# The below statement
# is VALID as lists
# are mutable
strList[0] = 'b'
Type C
1 . str = input("Enter the string: ")
ch = input("Enter the character to count: ");
c = str.count(ch)
print(ch, "occurs", c, "times"
2 . 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)
3 . str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr = ch + newStr
print(newStr)
4 . phNo = input("Enter the phone number: ")
length = len(phNo)
if length == 12 \
and phNo[3] == "-" \
and phNo[7] == "-" \
and phNo[:3].isdigit() \
and phNo[4:7].isdigit() \
and phNo[8:].isdigit() :
print("Valid Phone Number")
else :
print("Invalid Phone Number")
5 . str = input("Enter the string: ")
sum = 0
digitStr = ''
for ch in str :
if ch.isdigit() :
digitStr += ch
sum += int(ch)
if not digitStr :
print(str, "has no digits")
else :
print(str, "has the digits", digitStr, "which sum to", sum)
6 . str = input("Enter a few sentences: ")
length = len(str)
spaceCount = 0
alnumCount = 0
for ch in str :
if ch.isspace() :
spaceCount += 1
elif ch.isalnum() :
alnumCount += 1
alnumPercent = alnumCount / length * 100
print("Original Sentences:")
print(str)
print("Number of words =", (spaceCount + 1))
print("Number of characters =", (length + 1))
print("Alphanumeric Percentage =", alnumPercent)
7 . while True :
str = input("Please enter a sentence, or 'q' to quit : ")
newStr = ""
if str.lower() == "q" :
break
for ch in str :
if ch.islower() :
newStr += ch.upper()
elif ch.isupper() :
newStr += ch.lower()
else :
newStr += ch
print(newStr)
8 . num = int(input("Enter an integer: "))
str = input("Enter the string: ")
digitsStr = ''
digitsNum = 0;
for ch in str :
if ch.isdigit() :
digitsStr += ch
if digitsStr :
digitsNum = int(digitsStr)
print(num, "+", digitsNum, "=", (num + digitsNum))
9 . str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
small = str1
large = str2
if len(str1) > len(str2) :
large = str1
small = str2
print(small)
lenLarge = len(large)
for i in range(lenLarge // 2) :
print(' ' * i, large[i], ' ' * (lenLarge - 2 * i), large[lenLarge - i - 1], sep='')
10 . n = int(input("Enter the number: "))
num = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
rom = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
result = ''
for i in range(len(num)) :
count = int(n / num[i])
result += str(rom[i] * count)
n -= num[i] * count
elif ch == ')' :
count -= 1
if count == 0 :
print("Formula has same number of opening and closing parentheses")
else :
print("Formula has unequal number of opening and closing parentheses")
13 . str = input("Enter a string: ")
count = 0
for ch in str :
lch = ch.lower()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
count += 1
print("Vowel Count =", count)
14 . str = input("Enter a string: ")
words = str.split()
longWord = ''
for w in words :
if len(w) > len(longWord) :
longWord = w
print("Longest Word =", longWord)
15 . str = input("Enter a string: ")
words = str.split()
newStr = ""
for w in words :
rw = ""
for ch in w :
rw = ch + rw
newStr += rw + " "
print(newStr)