Q. Write a Python program having following functions:
(i) A function with the following signature:
remove_letter (sentence, letter)
This function should take a string and a letter (as a single-character string) as arguments returning a copy of that string with every instance of the indicated letter removed
For example remove_letter ("Hello there!", "e") should return the string "Hllo thr!". Try implementing it using <str>.split ( ) function.
(ii) Write a function to do the following :
Try implementing the capwords () functionality using other functions, i.e , split(), capitalize () and join( ). Compare the result with the capwords() function's result.
You can understand by Watching video :-
Answer :-
(i)
def remove_letter( sentence , letter ) : new_sentence = "" sen = sentence.split ( letter ) for i in sen : new_sentence += i return new_sentence sentence = input("Enter a sentence = ") letter = input("Enter a letter = ") print ( remove_letter (sentence , letter) )
(ii)
def capwords( sentence ) :
sentan = [ ]
sen = sentence.split()
for i in sen :
word = ""
for j in i :
word += j.capitalize ()
sentan += [ word ]
new_sentence = " ".join(sentan)
return new_sentence
sentence = input("Enter a sentence = ")
print("Capital word = " , capwords(sentence))
print(" by upper method",sentence.upper())
Output :-
(i)
Enter a sentence = Hello there!
Enter a letter = e
Hllo thr!
>>>
Enter a sentence = This is Path Walla Website.
Enter a letter = Walla
This is Path Website.
>>>
Enter a sentence = We are going to do some Python code
Enter a letter = to
We are going do some Python code
>>>
(ii)
Enter a sentence :- This is the Path Walla Website.
Capital word :- THIS IS THE PATH WALLA WEBSITE.
By using upper Function :- THIS IS THE PATH WALLA WEBSITE.
>>>
Enter a sentence :- We are going to do some Python code
Capital word :- WE ARE GOING TO DO SOME PYTHON CODE
By using upper Function :- WE ARE GOING TO DO SOME PYTHON CODE
>>>
Second one is wrong
ReplyDeleteWhy, Please watch video for better understanding.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )