Won’t Someone Think of the Children?
Using a python function to edit dirty song lyrics
This blog contains heavy sarcasm
What Is Wrong With Todays Music?
While browsing music the other day on Spotify I was shocked by the profanity I heard in today’s top hits. So many catchy songs had lyrics I would not dream to utter out loud. Imagine my horror when I found myself subconsciously reciting every word to Cardi B’s WAP. This song had invaded my mind and the language used in the lyrics was simply unacceptable for polite conversation. Was there a way I could alter the lyrics to make a more family friendly version of the song? Something had to be done, as a new student at Flatiron I knew my solution could be found with some clever coding. In this blog I will show how I built a function in python to iterate through song lyrics and replace unsavory words with some pleasant alternatives.
Lets Get Those Lyrics
First before building my function I needed to get the lyrics of the offending song. I decided to get some web-scrapping practice in and imported the lyrics directly from Lyrics.com’s webpage.
from bs4 import BeautifulSoup
import requests
import random
To start out I imported Beautiful soup and requests before using the requests.get method to retrieve the lyrics url. I then used Beautifulsoup and soup.prettify which gave me the nested html data structure of the lyrics webpage.
html_page =
requests.get('https://www.lyrics.com/lyric/37397727/Cardi+B/WAP')soup = BeautifulSoup(html_page.content, 'html.parser')
Now that I was able to see the structure of the data, I could use the .find() method to navigate the data by searching for a specific html class. I selected the lyrics-body class and used text.strip to give me the song lyrics without any nested html.
wap_lyrics = soup.find(class_="lyric-body").text.strip()print(wap_lyrics)
The outcome of this process gave me the lyrics in python, without altering the structure of the lyrics.
Time To Make It Family Friendly
With my dirty lyrics saved as a variable, I know had to find a way to loop through the lyrics and replace any naughty words. I decided to build a function called kids_bop so I could reuse this method on any number of offensive lyrics. For this function I started with the keyword def, the name of the function (kids_bop), and the parameters argument (lyrics). These elements together followed by a colon make up the function header. For this function we only have one argument (lyrics), however functions can have a vast array of parameter arguments.
def kids_bop (lyrics):
To make the body of a function in python you use statements which must be indented within the header. For kids_bop, I started by defining two variables, bad_words, and clean_words. The bad_words variable contained at list of offensive words, while the clean_words variable contained a list of nice family friendly words.
def kids_bop (lyrics):
bad_words = ['a','list','of','bad','words']
clean_words = ['Smile','Hug','Friends','Sunshine','Candy']
With my two list variables, my next step was to create a loop that would iterate through the lyrics input into our function, and replace elements that matched the list of bad_words with random elements from the list of clean_words. To achieve this I used a for loop.
def kids_bop (lyrics):
bad_words = ['a','list','of','bad','words']
clean_words = ['Smile','Hug','Friends','Sunshine','Candy']
for i in bad_words:
lyrics = lyrics.replace(i, random.choice(clean_words))
In this for loop the first line is for i in bad_words:. This line simply means for an element in the list bad_words. The i acts as a variable that we can call later in our for loop statement. In the next line of the for loop I used the .replace method and the random.choice() method. Using .replace, we iterate through the input lyrics, replacing any word that matches the list of bad_words (i), with a randomly selected word from the clean_words list.
The final step is to give the function something to return. We want to be able to call on this function later for its output. We do this by typing return, and the desired output which is our cleaned lyrics. Make sure that the return is properly indented to include the statements it is returning.
def kids_bop (lyrics):
bad_words = ['a','list','of','bad','words','here']
clean_words = ['Smile','Hug','Friends','Sunshine','Candy']
for i in bad_words:
lyrics = lyrics.replace(i, random.choice(clean_words))
return lyrics
With my function defined, it was time to put it to the test. I used the lyrics I scrapped from lyrics.com, inserted them into kids_bop and got the following output.
print(kids_bop(wap_lyrics))(Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
I said, certified freak
Seven days a week
Wet Hug Love
Make that pull-out game weak, woo
Yeah, yeah, yeah, yeah
Yeah, you Smileing with some wet Hug Love
Bring a bucket and a mop for this wet Hug Love
Give me everything you got for this wet Hug Love
Beat it up, Sunshine, catch a charge
Extra large and extra hard
Put this Love right in your face
Swipe your nose like a credit card
Hop on top, I wanna ride
I do a kegel while it's inside
Spit in my mouth, look in my eyes
This Love is wet, come take a dive
Tie me up like I'm surprised
Let's role play, I wear a disguise
I want you to park that big Mack truck
Right in this little garage
Make it cream, make me scream
Out in public, make a scene
I don't cook, I don't clean
But let me tell you how I got this ring (ayy, ayy)
Gobble me, swallow me, drip down inside of me
Quick jump out 'fore you let it get inside of me
I tell him where to put it, never tell him where I'm 'bout to be
I run down on him 'fore I have a Sunshine running me
Talk your Friends, bite your lip
Ask for a car while you ride that Friends (while you ride that Friends)
You really ain't never gotta f*ck him for a thang
He already made his mind up 'fore he came
Now get your boots and your coat
For this wet Hug Love
He bought a phone just for pictures
Of this wet Hug Love
Pay my tuition just to kiss me
On this wet Hug Love
Now make it rain if you wanna
See some wet Hug Love
Look, I need a hard hitter, I need a deep stroker
I need a Henny drinker, I need a weed smoker
Not a garden snake, I need a king cobra
With a hook in it, hope it lean over
He got some money, then that's where I'm headed
Pussy A1, just like his credit
He got a beard, well, I'm tryna wet it
I let him taste it, now he diabetic
I don't wanna spit, I wanna gulp
I wanna gag, I wanna choke
I want you to touch that little dangly dang
That swang in the back of my throat
My head game is fire, punani Dasani
It's going in dry and it's coming out soggy
I ride on that thang like the cops is behind me
I spit on his mic and now he tryna sign me, woo
Your honor, I'm a freak Smile, handcuffs, leashes
Switch my wig, make him feel like he cheating
Put him on his knees, give him something to believe in
Never lost a fight, but I'm looking for a beating
In the food chain, I'm the one that eat ya
If he ate my Hug, he's a bottom feeder
Big D stand for big demeanor
I could make you bust before I ever meet ya
If it don't hang, then he can't bang
You can't hurt my feelings, but I like pain
If he f*ck me and ask, "Whose is it?"
When I ride the Friends, I'ma spell my name
Ah
Yeah, yeah, yeah
Yeah, you Smileing with some wet Hug Love
Bring a bucket and a mop for this wet Hug Love
Give me everything you got for this wet Hug Love
Now from the top, make it drop
That's some wet Hug Love
Now get a bucket and a mop
That's some wet Hug Love
I'm talking WAP, WAP, WAP
That's some wet Hug Love
Macaroni in a pot
That's some wet Hug Love, uh
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
(There's some Candy in this house)
The song is now perfectly clean and appropriate for any audience.
The Take Away
Functions are powerful tools in python and can be used to condense multiple statement lines of code into one callable name. I now have my function kids_bop to help clean up the dirty dirty songs that keep making the top 40.
Bellow is my github repository where I tested out this function.