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 onstring1
, passing instring2
and the literal string"***"
.
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 thesplit
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.
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'.
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!
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".