Python Programming Questions - Operation on STRING[SET-2]
1. Python program to print even length words in a string
stringInput = input(" Enter The String : ")
stringInput = input(" Enter The String : ")
word = stringInput.split(' ')
for i in word:
if(len(i)%2 == 0):
print("",i)
count = int(input("\n\n Enter The Number Of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(input())
for words in myList:
if(len(words)%2 == 0):
print("",words)
else:
pass
for i in word:
if(len(i)%2 == 0):
print("",i)
count = int(input("\n\n Enter The Number Of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(input())
for words in myList:
if(len(words)%2 == 0):
print("",words)
else:
pass
Enter The String : Python Is A Programming Language
Python
Is
Language
Enter The Number Of Elements Present In List : 5
Python
JAVA
C++
HTML
CSS
Python
JAVA
HTML
Python
Is
Language
Enter The Number Of Elements Present In List : 5
Python
JAVA
C++
HTML
CSS
Python
JAVA
HTML
2. Python Program to accept the strings which contains all vowels
inputString = input(" Enter The String : ")
low = inputString.lower()
vowels = set("aeiou")
emptyset = set()
for i in low:
if i in vowels:
emptyset.add(i)
else:
pass
if(len(emptyset) == len(vowels)):
print("",inputString," Accepted ")
else:
print("",inputString," Not Accepted ")
count = int(input("\n\n Enter The Number Of Elements Present In List : "))vowelSet = set("aeiou")
emptySet = set()
myList = []
vowelsList = []
for j in range(count):
myList.append(input())
flag = 0
for j in myList:
low = j.lower()
for k in range(len(low)):
if low[k] in vowelSet:
emptySet.add(low[k])
if(len(emptySet) == len(vowelSet)):
flag = 1
else:
pass
if(flag == 1):
vowelsList.append(j)
print(vowelsList)
low = inputString.lower()
vowels = set("aeiou")
emptyset = set()
for i in low:
if i in vowels:
emptyset.add(i)
else:
pass
if(len(emptyset) == len(vowels)):
print("",inputString," Accepted ")
else:
print("",inputString," Not Accepted ")
count = int(input("\n\n Enter The Number Of Elements Present In List : "))vowelSet = set("aeiou")
emptySet = set()
myList = []
vowelsList = []
for j in range(count):
myList.append(input())
flag = 0
for j in myList:
low = j.lower()
for k in range(len(low)):
if low[k] in vowelSet:
emptySet.add(low[k])
if(len(emptySet) == len(vowelSet)):
flag = 1
else:
pass
if(flag == 1):
vowelsList.append(j)
print(vowelsList)
Enter The String : Education
Education
Accepted
Enter The Number Of Elements Present In List : 3
Python
Education
AEEiioU
['Education', 'AEEiioU']
Accepted
Enter The Number Of Elements Present In List : 3
Python
Education
AEEiioU
['Education', 'AEEiioU']
3. Python Program To Count the Number of matching characters in a pair of string
inputString = input(" Enter The String : ")
inputSubStr = input(" Enter The Substring : ")
#METHOD1
inputStringSet = set()
inputSubstringSet = set()
for i in inputString:
inputStringSet.add(i)
for j in inputSubStr:
inputSubstringSet.add(j)
matched = inputStringSet & inputSubstringSet
print(" By Method1 Number Of Matching Character ",len(matched))
#METHOD2
count = 0
j = 0
for i in inputString:
if(inputSubStr.find(i) >= 0 and j == inputString.find(i)):
count = count + 1
j = j+1
print(" By Method2 Number Of Matching Character ",count)
inputSubStr = input(" Enter The Substring : ")
#METHOD1
inputStringSet = set()
inputSubstringSet = set()
for i in inputString:
inputStringSet.add(i)
for j in inputSubStr:
inputSubstringSet.add(j)
matched = inputStringSet & inputSubstringSet
print(" By Method1 Number Of Matching Character ",len(matched))
#METHOD2
count = 0
j = 0
for i in inputString:
if(inputSubStr.find(i) >= 0 and j == inputString.find(i)):
count = count + 1
j = j+1
print(" By Method2 Number Of Matching Character ",count)
Enter The String : EDUCATION
Enter The Substring : CATE
By Method1 Number Of Matching Character 4
By Method2 Number Of Matching Character 4
Enter The Substring : CATE
By Method1 Number Of Matching Character 4
By Method2 Number Of Matching Character 4
4. Python program to count number of vowels using sets in given string
inputString = input(" Enter The String : ")
low = inputString.lower()
vowels = set("aeiou")
count = 0
for i in low:
if i in vowels:
count = count + 1
print(" Number Of Vowels Present In ",inputString, " Is : ",count\)
low = inputString.lower()
vowels = set("aeiou")
count = 0
for i in low:
if i in vowels:
count = count + 1
print(" Number Of Vowels Present In ",inputString, " Is : ",count\)
Enter The String : Education
Number Of Vowels Present In Education Is : 5
5.Python Program To Remove all duplicates from a given string
from collections import OrderedDict
def removeDuplicateWithOrd(inputString):
return "".join(OrderedDict.fromkeys(inputString))
inputString = input(" Enter The String : ")
outputString = removeDuplicateWithOrd(inputString)
print(" Entered String Before Removing Duplicate :",inputString)
print(" Entered String Afteer Removing Duplicate :",outputString)
Enter The String : Python Is A Programming Language
Entered String Before Removing Duplicate : Python Is A Programming Language
Entered String Afteer Removing Duplicate : Python IsArgamiLue
def removeDuplicateWithOrd(inputString):
return "".join(OrderedDict.fromkeys(inputString))
inputString = input(" Enter The String : ")
outputString = removeDuplicateWithOrd(inputString)
print(" Entered String Before Removing Duplicate :",inputString)
print(" Entered String Afteer Removing Duplicate :",outputString)
Enter The String : Python Is A Programming Language
Entered String Before Removing Duplicate : Python Is A Programming Language
Entered String Afteer Removing Duplicate : Python IsArgamiLue
Comments
Post a Comment