Exercise: String operations

Try out different string operations and methods in the following coding exercises. You probably want to keep the previous articles open in other tabs, as well as the Python string methods documentation.

String methods

The next two exercises help you practice using string methods.

Bleeper

Implement bleep_out, a function that looks for a particular phrase in a string and returns a new string with that phrase replaced by '***'.

If you'd like more guidance, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should use the replace method on string1, passing in string2 and the literal string "***".
def bleep_out(string1, string2): """Returns a version of string1 where all instances of string2 have been replaced by "***". >>> bleep_out('i freaking love carrots', 'freaking') 'i *** love carrots' >>> bleep_out('oh dang oh dang oh dang', 'dang') 'oh *** oh *** oh ***' """ return "" # REPLACE THIS LINE

Phone number formats

Implement phone_number_versions, a function that turns a hyphen-separated phone number into a list containing that original number, a space-separated version of it, and a period-separated version.

If you'd like more guidance, expand this block.
  • As a first step, your function should split phone_number into parts, using the split method, and store the results in a list.
  • Then, the function should create a new list, where some of the items are made using the join method.
def phone_number_versions(phone_number): """ Accepts a hyphen-separated phone number and returns a list containing the original version, a space-separated version, and a period-separated version. >>> phone_number_versions('1-800-867-5309') ['1-800-867-5309', '1 800 867 5309', '1.800.867.5309'] """ return [] # REPLACE THIS LINE

String slicing

The next three exercises help you practice using string slicing.

Mix up

Implement mix_up, a function that concatenates two words, but swaps out the first 2 characters of each word. For example, 'mix' and 'pod' is turned into 'pox mid'.

def mix_up(word1, word2): """Returns the concatenation of word1 and word2 separated by a space, with the first 2 characters of each word swapped. Assumes the two words are at least 2 characters long. >>> mix_up('mix', 'pod') 'pox mid' >>> mix_up('dog', 'dinner') 'dig donner' """ return "" # REPLACE THIS LINE

Gerundio

Implement gerundio, a function that transforms Spanish infinite verbs (such as "cantar" or "volver") into their gerund forms ("cantando" and "volviendo"). See the docstring for the transformation rules.

Once you get the initial tests passing, you can also implement this function for another language you know that has similar infinitive → gerund verb form changes. Share it with the rest of us if you do!

def gerundio(verb): """Turns Spanish infinitive verbs (which end in "ar", "er", "ir") into the gerund forms (which end in "ando" or "iendo). When a verb ends in "ar", the "ar" is replaced with "ando". When a verb ends in "er" or "ir", the end is replaced with "iendo". >>> gerundio('cantar') 'cantando' >>> gerundio('volver') 'volviendo' >>> gerundio('abrir') 'abriendo' >>> gerundio('armar') 'armando' """ return '' # REPLACE THIS LINE

Not bad? Good!

Implement not_bad, a function that replaces a phrase in a string that starts with "not" and ends with "bad" into the single word "good".

def not_bad(sentence): """Returns a new string where the first substring in a sentence that starts with "not" and ends with "bad" is replaced by the word "good". Returns the original string if no matching substring is found. >>> not_bad('This dinner is not that bad!') 'This dinner is good!' >>> not_bad('This movie is not so bad!') 'This movie is good!' >>> not_bad('This dinner is bad!') 'This dinner is bad!' >>> not_bad('This movie is not so bad, dont you think?') 'This movie is good, dont you think?' """ return '' # REPLACE THIS LINE
➡️ Next up: Dictionaries