Read the docs: Python string methods
Hello, 1st post here in medium, great idea. Allow me to cut to the chase.
I’m writing here to capture some realisations I’m getting as I move inwards into hands-on data wrangling. I will rely on Python as programming language. I’ll be needing a way to represent code nicely, so that it can be copy-pasted effortlessly, so I’ll be doing this:
The topic of this post is on ‘why a reluctance to use standard libraries holds you back’. If you are used to hack your way around to get tailored solutions, you will quickly realise that this behaviour doesn’t scale. An example of this is taking a string that contains a sentence and make a list of words out of it. To achieve this, you could just use the split string method using “ “ as the separator and keep the list you get in return.
Pretty good. Just make all lower case, remove the ‘.’ in the last element of the list and you are done. We can confidently define a function to automate this and call it a day:
It worked! the thing is, if you test this function (always test), you need to imagine that whoever is providing the input sentence is your enemy. So let’s see what the enemy can do:
Wow, pretty bad. And no errors are returned.. so you wouldn’t know something is wrong unless you checked. The enemy was able to defeat us very easily. So, now we know we need to come up with a more general solution. You could be inclined to roll up your sleeves and start coming up with tons of IF conditions to watch out for double spaces, or for punctuation in strange places.. but that’s a path that may lead to making a bad & complicated function for something that smarter people have already made for you. It’s called the standard library for Python. Here! friend, use this link.
Get AleAlejandro1’s stories in your inbox
Join Medium for free to get updates from this writer.
Using string methods like split, lower and replace as well as list methods like remove we are able to defeat our enemy’s last attack (pass tests) by doing:
Is this it? Is this the ultimate function to extract words from a sentence?. Definitely not. However, I’ve noticed two things I want to share:
a) Relying on methods makes the code neat and simple. No bells and whistles required. Trust that the smart people that invented split know what they are doing. Their code has been thoroughly tested.
b) A success factor for code is passing tests. It will allow you to make sure the code does what you want it to do. The less new lines you type the better, since every line is an opportunity to insert a weakness that your enemies can exploit. With this idea of building on top of good work from others, it feels like a good time to quote Newton:
“If I have been able to see further, it was only because I stood on the shoulders of giants.” -Isaac Newton
TL;DR :
1- Look for standard methods and functions before you write your own.
2- Test every function you define before you plug it into something else.
Bye for now









