Question

Write a Python function that will take a string and a list as its only parameters....

Write a Python function that will take a string and a list as its only parameters. The function needs to return a new string that is the original string with all of the characters appearing in the list parameter replaced with an asterisk(*). The returned string needs to have the remaining characters appear in the same location as the characters they replaced.

Hint/Reminder: Note you can make a string out of characters optimally without repeatedly using the concatenation operator.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the complete Python code: (I have added explanation of each line in the comment besides that)

def mark(word,l):

  """ This function replaces all the occurences of characters in l in word with a '*' """

  s = list(word) # We work with lists instead of strings as strings are immutable!

  for i in range(len(s)):

    if(s[i] in l): # is l contains the char replace it

      s[i] = '*'

  return "".join(s) # convert back to string

Notice that We don't need to use the concat operator multiple times and hence it is a very efficient code.

Screenshot of Code and Testing Output:
def mark(word, 1): This function replaces all the occurences of characters in 1 in word with a * s = list (word) # We wo

P.S. You can comment below the answer, for any doubts, and I will be happy to help!

Please give a thumbs up if my answer could be of help!

All the best!

Add a comment
Know the answer?
Add Answer to:
Write a Python function that will take a string and a list as its only parameters....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that...

    Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that takes an input string, and returns a the unscrambled version of the argument. To unscramble the string: When the string has an odd number of characters, middle character is the first character in the unscrambled result Pairs of remaining characters are added to the result, proceeding left to right from inner-most to outer-most characters. Example Call: unscramble('3cis1') Expected result: ics31 Example Call: unscramble('ocicssol') Expected...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • Python: Write a func that will create a list of random numbers. The function will take...

    Python: Write a func that will create a list of random numbers. The function will take in two parameters. The first parameter specifies how many values to generate and the second parameter is the highest random # (2 is the minimum) that the func generates. To get the random numbers, you may use this line of code: y = random.randint (2,max), where max is the highest possible random #, INCLUSIVE.

  • ** In Python ** Write a function named "tweets" that takes a string as a parameter...

    ** In Python ** Write a function named "tweets" that takes a string as a parameter and returns the number of tweets required to tweet the input to the world. Note: The maximum length for a single tweet is 280 characters Please provided an explanation.

  • Python 3.0 Please Write a function that takes, as arguments, a binary string, a list of...

    Python 3.0 Please Write a function that takes, as arguments, a binary string, a list of characters and the corresponding Huffman code for those characters. It decodes the binary string using the Huffman code, and returns the resulting decoded string. Name this function decodeStringHuffman(binaryString, myCharacters, myCode). For example, >>>decodeStringHuffman("1001100010111010101010111", ["A", "B", "C"], ["1", "00", "01"]) should return the string 'ABAABCCAACCCCCAA'

  • #Write a function called next_fib. next_fib should take #have two parameters: a list of integers and...

    #Write a function called next_fib. next_fib should take #have two parameters: a list of integers and a single integer. #For this description, we'll call the single integer n. # #next_fib should modify the list to add the next n pseudo- #Fibonacci numbers to the end of the sequence. A pseudo- #Fibonacci number is the sum of the previous two numbers in #the sequence, but in our case, the previous two numbers may #not be the original numbers from the Fibonacci...

  • Write a recursive function called abb pattern with a single parameter astr, which is a string....

    Write a recursive function called abb pattern with a single parameter astr, which is a string. The function returns True if astr is a string of the form a nb 2n (n a-s followed by 2n b-s, where n is a positive integer) and False otherwise. For example, abb pattern("abb"), abb pattern("aabbbb"), and abb pattern("aaaabbbbbbbb") all return True, but abb pattern("") (parameter is an empty string), abb pattern("abbabb"), and abb pattern("aaabbbbb") all return False. you may not use any built-in...

  • Need in Python! Write a function definition named  power  that takes two formal parameters which can be assumed...

    Need in Python! Write a function definition named  power  that takes two formal parameters which can be assumed to be any numeric type. The first parameter is called base, the second is called exponent. The function should return the result of the base raised to the exponent power.    You may not use any Python math library functions inside your function. You may create your own code to test the function in an IDE if you wish.   HINT: The exponent operator in...

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

  • Using Python, write a function named add_surname that takes as a parameter a list of first...

    Using Python, write a function named add_surname that takes as a parameter a list of first names. **It should use a list comprehension** to return a list that contains only those names that start with a "K", but with the surname "Kardashian" added to each one, with a space between the first and last names. For example, if the original list is: ``` ["Kiki", "Krystal", "Pavel", "Annie", "Koala"] ``` Then the list that is returned should be: ``` ['Kiki Kardashian',...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT