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...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • Write a python program that contains a hardcoded string and checks whether it is a palindrome...

    Write a python program that contains a hardcoded string and checks whether it is a palindrome (the same forwards as backwards). If it is then display “It is a palindrome”, otherwise display “It is not a palindrome”. A hardcoded string is simply a string defined in your program, for example: my_string = “Hello” Some sample palindromes to use are: A car, a man, a maraca Desserts, I stressed Madam, I’m Adam You will need to strip out any punctuation such...

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • Python The Python "<" and ">" comparison operators can be used to compare which string variable...

    Python The Python "<" and ">" comparison operators can be used to compare which string variable has a greater value based on comparing the ASCII codes of the characters in each string, one by one. To take some examples: "tets" > "test" returns True because the third letter of the first string, "t", has a greater value than the third letter of the second string, "s". "testa" > "test" returns True because—while the first four letters in both words are...

  • Write a Python function has_subsequence(a,s) that determines if the list (or string), a, contains s as...

    Write a Python function has_subsequence(a,s) that determines if the list (or string), a, contains s as a (not necessarily consecutive) subsequence. Examples 'here we go again' contains 'we gain' as a subsequence. 'green eggs and ham sandwhiches' contains 'reggae' as a subsequence. Your function should return an array of indices that shows the location of the elements of s in a. In the 'here we go again' example this array would contains [5,6,12,13,14,15]. If you can't find s as a...

  • Write a function in python that will take in a df (dataframe) and n (integer that...

    Write a function in python that will take in a df (dataframe) and n (integer that determines the number of rows that the returned df will display): create a new dataframe df1 by copying the input df, with only two columns "fruit" and "price" from the original df. Then add two new columns to df1 labeled "brand" and "time of purchase" and assign values appropriately. Return df1 NOTE: If n= -1, return all the rows. If n ≥ 0 return...

  • Write a function named "indexed_kvs" that doesn't take any parameters and returns a new key-value store...

    Write a function named "indexed_kvs" that doesn't take any parameters and returns a new key-value store containing the integers from 0 to 29 as values each stored at a key which is a string containing the digits of the integer. For example the key-value "0":0 will be in your returned key-value store (include both 0 and 29 in your list)

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
ADVERTISEMENT