-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperations on Strings.py
88 lines (66 loc) · 2.31 KB
/
Operations on Strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#Program to perform operations on an input string
print("This is a program to perform certain operations on an input string")
inp_list = input("Enter a sentence or a phrase: ").split()
while True:
print("\nA: Count number of words in input string")
print("B: Display frequency of each appearing word")
print("C: Search for a particular word")
print("E: Exit the program")
choice = input("\nEnter a choice(A/B/C/E): ")
if choice.upper() == "A":
print("The para has",len(inp_list),"words")
elif choice.upper() == "B":
count = 0
for i in inp_list:
count = inp_list.count(i)
print(i,count,sep = "\t",end = "\n")
elif choice.upper() == "C":
x = input("Enter a word: ")
if x in inp_list:
print("The word",x,"has been found at index",(inp_list.index(x)) + 1)
elif x not in inp_list:
print("The word",x,"was not found in the para.")
elif choice.upper() == "E":
print("The Program will end now! Thank you!")
break
else:
print("Enter a correct choice!")
continue
"""OUTPUT:
This is a program to perform certain operations on an input string
Enter a sentence or a phrase: I love igpay atinlay
A: Count number of words in input string
B: Display frequency of each appearing word
C: Search for a particular word
E: Exit the program
Enter a choice(A/B/C/E): a
The para has 4 words
A: Count number of words in input string
B: Display frequency of each appearing word
C: Search for a particular word
E: Exit the program
Enter a choice(A/B/C/E): b
I 1
love 1
igpay 1
atinlay 1
A: Count number of words in input string
B: Display frequency of each appearing word
C: Search for a particular word
E: Exit the program
Enter a choice(A/B/C/E): c
Enter a word: love
The word love has been found at index 2
A: Count number of words in input string
B: Display frequency of each appearing word
C: Search for a particular word
E: Exit the program
Enter a choice(A/B/C/E): d
Enter a correct choice!
A: Count number of words in input string
B: Display frequency of each appearing word
C: Search for a particular word
E: Exit the program
Enter a choice(A/B/C/E): e
The Program will end now! Thank you!
"""