• Home
  • Exam Papers
  • Notes
  • Python Labs

Lab 6

String/Tuple Assignment

Ques 1

sTr = input("Enter the string :")
c = 0 
for i in sTr.lower():
    if i in ['a','e','i','o','u']:
        c += 1
print("No. of vowels in the given string is",c)

Ques 2

sTr = input("Enter the string :")
rev = ""
for i in sTr:
    rev = i + rev 
    
print("Reversed string :",rev)

Ques 3

sTr = input("Enter the string :")
new_sTr = ""
for i in sTr:
    if i == " ":
        new_sTr += "_"
    else:
        new_sTr += i 
print("New String :",new_sTr)

Ques 4

sTr = input("Enter the string :")
L = []
for i in sTr:
    if i not in L:
        print("The count of",i,"in the string :",sTr.count(i))
        L.append(i)

Ques 5

sTr = input("Enter the string :")
L = sTr.split(" ")
largest = L[0]
for i in L:
    if len(i) > len(largest):
        largest = i
    
print("The largest word in the string is :",largest)

Ques 6

sTr = input("Enter the main string: ")
subS = input("Enter the substring to find: ")

index = -1
for i in range(len(sTr)):
    if sTr[i:i + len(subS)] == subS:
        index = i
        break

if index != -1:
    print("The first occurrence of",subS,"is at index",index)
else:
    print(subS,"not found in the string.")

Ques 7

sTr = input("Enter the string :")
for i in range(len(sTr)):
    if sTr[i]!=sTr[len(sTr)-i-1]:
        print("The given string is not palindrome.")
        break
else:
    print("The given string is palindrome.")

Ques 8

values = tuple(map(int, input("Enter the tuple values separated by spaces: ").split()))
maX = values[0]
miN = values[0]

for value in values:
    if value > maX:
        maX = value
    if value < miN:
        miN = value

print("Maximum value:",maX)
print("Minimum value:",miN)

Ques 9

# Input the tuple
values = tuple(map(int, input("Enter the tuple values separated by spaces: ").split()))
Sum = 0

for i in values:
    Sum += i    

print("The sum of all numeric elements in the tuple is:",Sum)

Ques 10

# Input the tuple