Python – Useful String Operations Part 1

Python – Useful String Operations Part 1
June 27, 2017 Reece

Python LogoMore 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]

In practice:

# returns "bill"
# returns "bob"
# returns "llyb"


Comment below or contact me. Keep it classy.

1 Comment


  1. […] In the last post we looked at the basics of getting the left, right,  and mid parts out of strings with a simple set of functions that are amazingly not native to python. If you haven’t looked at it and want to, check it out here. […]

Leave a reply

Your email address will not be published. Required fields are marked *