Common String Operators in Python

Dale Wahl
3 min readOct 1, 2017

--

I am going to go over a few nifty functions and methods that will help you get rid of characters you don’t want, fix capitalization issues, replace characters, and take strings apart and put them back together again.

If you are new to Python, you will quickly realize how powerful it can be when tackling lots of data. Hell, if all you know is that strings contain your text and a bit about for or while loops, you can probably figure out how to handle a lot of the problems you’ll run into.

One of the first tasks I had to figure out was how to remove ‘white space’ from before and after a list of strings containing text. Like this:

Yup, just tell Python to loop over every string/block of text in the list and if it starts or ends with a blank space, get rid of it. Feeling high and mighty, I showed this off and was politely asked “Why didn’t you just use .strip()?”

Cause I didn’t know about it. Where was that guy an hour ago anyway? What follows is a list of some of stuff I only learned after banging my head around trying to do things much smarter people already programmed and optimized.

Let’s start with stripping.

Simple and super useful to clean up whitespace or any weird characters that end up around your text.

How about some quick capitalization work?

These four functions are probably all you’ve ever need, but there are more out there if you look. Python sees capital characters as different from lowercase so it is important to standardize your data before working with it.

Replacing characters in strings:

The method .replace( ) is super handy for replacing whole substrings in strings. It can replace words altogether with any other string of characters you want. I also show you an example using .translate(str.maketrans( )) which basically does a one for one replacement by character. Play around with that as it can be pretty helpful.

When all else fails, rip it apart and put it back together.

Sometimes the above functions and methods just aren’t enough to get you what you need. If you cannot find exactly what you need online, you can always take the whole thing apart and work with it element by element.

Using .split( ) and .join( ) you can take a string, turn it into a list to modify as you please, and then stick it right back together again.

Notice that .join( ) works a little differently than some of the others. It is a method on the string you want to add between the elements of the list, which is the argument you pass it. It just looks weird, so pay attention: 'probably_a_space'.join(list_of_strings_to_join). Hope that helps.

Alright! That’s all I have for you today. It should get you started cleaning up your datasets at the very least. If you have any questions, google them or ask Stack Overflow. You’ll get to know that place soon enough.

Or you could ask me. But know that I’ll probably just google the answer and then pass it off to you to sound smart.

Cheers.

--

--