Computer Science
Write 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.
Python
Python Libraries
2 Likes
Answer
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
Answered By
1 Like
Related Questions
Consider the following package
music/ Top-level package ├── __init__.py ├── formats/ Subpackage for file format │ ├── __init__.py │ └── wavread.py │ └── wavwrite.py │ ├── effects/ Subpackage for sound effects │ └── __init__.py │ └── echo.py │ └── surround.py │ └── reverse.py │ └── filters/ Subpackage for filters ├── __init__.py └── equalizer.py └── vocoder.py └── karaoke.py
Each of the above modules contain functions play(), writefile() and readfile().
(a) If the module wavwrite is imported using command import music.formats.wavwrite. How will you invoke its writefile() function ? Write command for it.
(b) If the module wavwrite is imported using command from music.formats import wavwrite. How will you invoke its writefile() function ? Write command for it.
What are the possible outcome(s) executed from the following code ? Also specify the maximum and minimum values that can be assigned to variable PICK.
import random PICK = random.randint(0, 3) CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]; for I in CITY : for J in range(1, PICK): print(I, end = " ") print()
- DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA - DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI - DELHI
MUMBAI
CHENNAI
KOLKATA - DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA
- DELHIDELHI
Create a module lengthconversion.py that stores functions for various lengths conversion e.g.,
- miletokm() — to convert miles to kilometer
- kmtomile() — to convert kilometers to miles
- feettoinches()
- inchestofeet()
It should also store constant values such as value of (mile in kilometers and vice versa).
[1 mile = 1.609344 kilometer ; 1 feet = 12 inches]
Help() function should display proper information.
Create a module MassConversion.py that stores function for mass conversion e.g.,
1.kgtotonne() — to convert kg to tonnes- tonnetokg() — to convert tonne to kg
- kgtopound() — to convert kg to pound
- poundtokg() — to convert pound to kg
(Also store constants 1 kg = 0.001 tonne, 1 kg = 2.20462 pound)
Help( ) function should give proper information about the module.