0% found this document useful (0 votes)
8 views7 pages

Python String and List Manipulation Programs

The document contains code snippets and sample input/output for several programs: 1) A menu-driven program that accepts a string and performs operations like reversing the string, checking if a character is present, removing vowels, and counting character occurrences. 2) A program that takes comma separated words as input and prints unique words in sorted order. 3) Programs that update, insert, delete elements from a list and rotate list elements left/right. 4) A program that stores word frequencies in a dictionary with words as keys and counts as values.

Uploaded by

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

Python String and List Manipulation Programs

The document contains code snippets and sample input/output for several programs: 1) A menu-driven program that accepts a string and performs operations like reversing the string, checking if a character is present, removing vowels, and counting character occurrences. 2) A program that takes comma separated words as input and prints unique words in sorted order. 3) Programs that update, insert, delete elements from a list and rotate list elements left/right. 4) A program that stores word frequencies in a dictionary with words as keys and counts as values.

Uploaded by

prajittokdar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

12. Aim: To write a program to print the largest word in a given string.

Program:
str1=input("Enter a string:")
words=[Link]()
print(words)
max=0
maxword=''
for i in words:
l=len(i)
if max<l:
max=l
maxword=i
print("Largest word:",maxword)

Input/Output:
Enter a string:Flowers are beautiful
['Flowers', 'are', 'beautiful']
Largest word: beautiful

13. Aim: To write a program to input comma separated words and prints unique words in sorted order.

Program:
words=eval(input("Enter comma separated sequence of words:"))
uwords=[]
for i in words:
if i not in uwords:
[Link](i)
print(uwords)
[Link]()
print("Unique words in sorted form:",uwords)

Input/Output:
Enter comma separated sequence of words:['van','cat','man','van']
['van', 'cat', 'man']
Unique words in sorted form: ['cat', 'man', 'van']

14. Aim: To write a menu driven program to accept a string and perform the following:
-Reverse a string
-Check given character is present or not
-Display string after removing vowels
-Count the occurrences of a character in a string
Program:
str1=input("Enter a string:")
while True:
print('''[Link]
[Link] is present
[Link] vowels
[Link]
[Link]''')
ch=int(input("Enter your choice:"))
if ch==1:
revstr=''
size=len(str1)
for i in range(-1, -(size+1), -1):
revstr+=str1[i]
print(revstr)
elif ch==2:
chr=input("Enter the character to be searched:")
i=0
size=len(str1)
while i<size:
if str1[i]==chr:
print(chr, "found in string at index:",i)
break
i+=1
else:
print(chr, "found not found in string ")
elif ch==3:
nstr=''
for i in str1:
if i not in "aeiouuAEIOU":
nstr+=i
print("String without vowels:",nstr)
elif ch==4:
chr=input("Enter the character to be searched:")
i=0
c=0
size=len(str1)
while i<size:
if str1[i]==chr:
c+=1
i+=1
print(chr, "found ",c,"times ")
elif ch==5:
break
else:
print("Invalid Input")
Input/Output:
Enter a string:This is a book
[Link]
[Link] is present
[Link] vowels
[Link]
[Link]
Enter your choice:1
koob a si sihT
[Link]
[Link] is present
[Link] vowels
[Link]
[Link]
Enter your choice:2
Enter the character to be searched:h
h found in string at index: 1
[Link]
[Link] is present
[Link] vowels
[Link]
[Link]
Enter your choice:3
String without vowels: Ths s bk
[Link]
[Link] is present
[Link] vowels
[Link]
[Link]
Enter your choice:4
Enter the character to be searched:i
i found 2 times
[Link]
[Link] is present
[Link] vowels
[Link]
[Link]
Enter your choice:5

15. Aim: To write a menu driven program to take list from the user and do the following
-Update value of the list
-Insert an element
-Delete an element
-Exit

Program:
l=eval(input("Enter list:"))
print("list is",l)
while True:
print('''[Link]
[Link]
[Link]
[Link]''')
ch=int(input("Enter choice:"))
if ch==1:
ele=int(input("Enter element to be modified:"))
nele=int(input("Enter new element:"))
for i in range(len(l)):
if l[i]==ele:
l[i]=nele
print("Modified List:",l)
elif ch==2:
ele=int(input("Enter element to be inserted:"))
[Link](ele)
print("Modified List:",l)
elif ch==3:
ele=int(input("Enter element to be deleted:"))
for i in range(len(l)):
if l[i]==ele:
del l[i]
break
print("Modified List:",l)
elif ch==4:
break
else:
print("Invalid Input")

Input/Output:
Enter list:[10,20,30]
list is [10, 20, 30]
[Link]
[Link]
[Link]
[Link]
Enter choice:1
Enter element to be modified:20
Enter new element:40
Modified List: [10, 40, 30]
[Link]
[Link]
[Link]
[Link]
Enter choice:2
Enter element to be inserted:50
Modified List: [10, 40, 30, 50]
[Link]
[Link]
[Link]
[Link]
Enter choice:3
Enter element to be deleted:30
Modified List: [10, 40, 50]
[Link]
[Link]
[Link]
[Link]
Enter choice:4

16. Aim: To write a program having nested tuple storing topper details, edit details and display the
details

Program:
toppers=(("Arya","BE",92),("Shyam","BSc",98),("Bala","BTech",97))
ntoppers=()
for i in toppers:
print(i)
ch=input("Do you want to edit details:")
if ch=='y' or ch=='Y':
name=input("Enter name of the student whose details are to be edited:")
nname=input("Enter new name:")
ncourse=input("Enter new course:")
naggr=input("Enter new aggregate:")
i=0
while i < len(toppers):
if toppers[i][0]==name:
ntoppers+=((nname,ncourse,naggr),)
else:
ntoppers+=(toppers[i],)
i+=1
for i in ntoppers:
print(i, end=' ')

Input/Output:
('Arya', 'BE', 92)
('Shyam', 'BSc', 98)
('Bala', 'BTech', 97)
Do you want to edit details:y
Enter name of the student whose details are to be edited:Arya
Enter new name:Ana
Enter new course:BE
Enter new aggregate:76
('Ana', 'BE', '76') ('Shyam', 'BSc', 98) ('Bala', 'BTech', 97)
17. Aim: To write a program to rotate the list elements

Program:
l=eval(input("Enter list:"))
while True:
print('[Link] left\[Link] right\[Link]')
ch=int(input("Enter choice:"))
if ch==1:
temp=l[0]
for i in range(len(l)-1):
l[i]=l[i+1]
l[len(l)-1]=temp
print(l)
elif ch==2:
temp=l[len(l)-1]
for i in range(len(l)-2,-1,-1):
l[i+1]=l[i]
l[0]=temp
print(l)
elif ch==3:
break
else:
print("Invalid Input")

Input/Output:
Enter list:[1,2,3,4]
[Link] left
[Link] right
[Link]
Enter choice:2
[4, 1, 2, 3]
[Link] left
[Link] right
[Link]
Enter choice:1
[1, 2, 3, 4]
[Link] left
[Link] right
[Link]
Enter choice:3
18. Aim: To write a program to find the frequency of words and store the words as dictionary keys and
frequency as values.

Program:
str1=input("enter string:")
words=[Link]()
print(str1)
print(words)
dict1={}
c=0
for i in words:
if i not in dict1:
dict1[i]=1
else:
dict1[i]+=1
print(dict1)

Input/Output:
enter string:it is a book and a pen
it is a book and a pen
['it', 'is', 'a', 'book', 'and', 'a', 'pen']
{'it': 1, 'is': 1, 'a': 2, 'book': 1, 'and': 1, 'pen': 1}

You might also like