0% found this document useful (0 votes)
2 views3 pages

Python String Manipulation Exercises

Uploaded by

nikamprajyot2
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)
2 views3 pages

Python String Manipulation Exercises

Uploaded by

nikamprajyot2
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

PYTHON STRING EXERCISE 2

1)Write the func on countB(word) which takes a word as the argument and returns the
number of ‘b’ in that word.

def countB(word): Output


print(word) abbbabbaaa
count=0 Number of 'b'= 5
for b in word:
if(b=='b'):
count=count+1
return count
print("Number of 'b'=",countB("abbbabbaaa"))

2)Write a func on which changes the case . Write a func on modify_Case(word) which
changes the case of all le ers in a word and returns the new word.

def modify_Case(word):
print("Original String=",word)
print("A er Swapping String=",end='')
return [Link]()
print(modify_Case("hi,python is interes ng,isn't it?"))
Output
Original String= hi,python is interes ng,isn't it?
A er Swapping String=HI,PYTHON IS INTERESTING,ISN'T IT?

3)Write a func on which returns a specified character. Write a func on getChar(word,pos)


which returns a character at that posi on.

def getChar(word,pos): Output


print("Word=",word) Word= Addicted to Python
print("Character at posi on ",pos,"=") Character at posi on 3 = i
counter=0
for i in word:
counter=counter+1
if(counter==pos):
return i
print(getChar("Addicted to Python",3))

4)Write func on which eliminates a le er from a word. Write a func on


Eliminate_Le er(word,le er) which takes a word and a le er as arguments and removes all
occurances of that par cular le er from that word and returns the remaining le ers in the
word.

1
def Eliminate_Le er(word,Le er): Output
print("String=",word) String= PYTHON PROGRAMMING
print("A er removing Le er:",Le er) A er removing Le er: P
print("String=",end="") String=YTHON ROGRAMMING
newstr=''
newstr=[Link](Le er,"")
return newstr
x=Eliminate_Le er('PYTHON PROGRAMMING','P')
print(x)

5)Write func on that count vowels in a word. Write a func on countVowels(word) which
takes a word as an argument and returns vowels(‘a’, ’e’, ’i’, ‘o’, ’u’) in that word.

def countVowels(word):
print("Word =",word)
word=[Link]()
return{v:[Link](v) for v in 'aeiou'}
print(countVowels("I love Python Programming"))
Output
Word= I love Python Programming
{'a': 1, 'e': 1, 'i': 2, 'o': 3, 'u': 0}
6)Write func on which returns the word with all the vowels capitalised.

def UppercaseVowels(word): Output


new='' string= aehsdfiou
print("string=", word) A er Capitalizing Vowels
print("A er Capitalizing Vowels") String=AEhsdfIOU
print("String=",end="")
for i in word:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u'):
new=new+[Link]()
else:
new=new+i
return new
x= UppercaseVowels('aehsdfiou')
print(x)
7)Write func on to remove vowels in a word and return remaining le ers

def removeVowels(word): OUTPUT


new='' String= abceiodeuf
print("String=", word) String A er removing Vowels=bcdf
print("String A er removing Vowels=", end='')
for i in word:
if (i!='a' and i!='e' and i!='i' and i!=’o’ and i!='u'):
new=new+i
return new
x=removeVowels('abceiodeuf')
print(x)
2
8)Write func on to check whether words are reverse of each other.

def isReverse(word1,word2): OUTPUT


print("First word=",word1) First word= Hello
print("Second word=",word2) Second word= olleH
if (word1==word2[::-1]): True
return True
else:
return false
x= isReverse('Hello','olleH')
print(x)
9)Write a func on which takes two words and return a new word by mirroring

def mirrorText(word1,word2): Output


print("String1=", word1) String1= Python
print("String2=", word2) String2= Strong
print("Mirror String=",end='') Mirror String=PythonStrongStrongPython
return (word1+word2+word2+word1)
x=mirrorText('Python','Strong')
print(x)

You might also like