More Python Quirks and Simple Solutions – String Operations – Part 1
I have a long history of working with Excel (12 years and counting as of this writing). As I transition to using Python more and more for analysis work, I keep finding quirks. Functionality that I am used to being basic in Excel that isn’t native in Python. I started with needing to use “Left” and “Right” operations on a string and then built a few more functions to fill some gaps for me.
I plan on this being a multi-part series, looking at more complicated functions as we go. A lot of this information is what I have gleaned from swimming through posts on StackOverflow.
Let’s take a look.
Basics – Left / Right / Mid
These are so basic, it was one of the first things I learned in Excel. They are also self-explanatory so I won’t bore you with the details.
def left(s, amount): return s[:amount] def right(s, amount): return s[-amount:] def mid(s, offset, amount): return s[offset:offset+amount]
print(left("billybob",4)) # returns "bill" print(right("billybob",3)) # returns "bob" print(mid("billybob",2,4)) # returns "llyb"
Comment below or contact me. Keep it classy.