Using Python Libraries — Question 1
Back to all questionsWrite a Python program having following functions :
- 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. - 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.
def remove_letter(sentence, letter):
words = sentence.split()
new_sentence = []
for word in words:
new_word = ''
for char in word:
if char.lower() != letter.lower():
new_word += char
new_sentence.append(new_word)
return ' '.join(new_sentence)
def capwords_custom(sentence):
words = sentence.split()
capitalized_words = []
for word in words:
capitalized_word = word.capitalize()
capitalized_words.append(capitalized_word)
return ' '.join(capitalized_words)
sentence = input("Enter a sentence: ")
letter = input("Enter a letter: ")
print("After removing letter:", remove_letter(sentence, letter))
from string import capwords
sentences_input = input("Enter a sentence: ")
sentences = sentences_input.split('.')
for sentence in sentences:
sentence = sentence.strip()
print("Custom capwords result:", capwords_custom(sentence))
print("Capwords result from string module:", capwords(sentence))Enter a sentence: Hello there!
Enter a letter: e
After removing letter: Hllo thr!
Enter a sentence: python is best programming language
Custom capwords result: Python Is Best Programming Language
Capwords result from string module: Python Is Best Programming Language
def
remove_letter
(
sentence
,
letter
):
words
=
sentence
.
split
()
new_sentence
=
[]
for
word
in
words
:
new_word
=
''
for
char
in
word
:
if
char
.
lower
()
!=
letter
.
lower
():
new_word
+=
char
new_sentence
.
append
(
new_word
)
return
' '
.
join
(
new_sentence
)
def
capwords_custom
(
sentence
):
words
=
sentence
.
split
()
capitalized_words
=
[]
for
word
in
words
:
capitalized_word
=
word
.
capitalize
()
capitalized_words
.
append
(
capitalized_word
)
return
' '
.
join
(
capitalized_words
)
sentence
=
input
(
"Enter a sentence: "
)
letter
=
input
(
"Enter a letter: "
)
print
(
"After removing letter:"
,
remove_letter
(
sentence
,
letter
))
from
string
import
capwords
sentences_input
=
input
(
"Enter a sentence: "
)
sentences
=
sentences_input
.
split
(
'.'
)
for
sentence
in
sentences
:
sentence
=
sentence
.
strip
()
print
(
"Custom capwords result:"
,
capwords_custom
(
sentence
))
print
(
"Capwords result from string module:"
,
capwords
(
sentence
))
Output
Enter a sentence: Hello there!
Enter a letter: e
After removing letter: Hllo thr!
Enter a sentence: python is best programming language
Custom capwords result: Python Is Best Programming Language
Capwords result from string module: Python Is Best Programming Language