Knowledge base

Writing Transforms in Python - Part 5

Working with Strings in Python - Python has a lot of useful features built into its data types, as we’ve seen with our record dictionaries, but strings (text values) can do a lot for us as well.

2 min read
Writing Transforms in Python - Part 5

Full Series

Part 1 - The Default Transform & Working with Records
Part 2 - Reading from Records
Part 3 - Writing to Records
Part 4 - Removing Fields from a Record
Part 5 - Working with Strings in Python


Working with Strings in Python

Python has a lot of useful features built into its data types, as we’ve seen with our record dictionaries, but strings (text values) can do a lot for us as well - Without writing any code, we can easily change the casing of a value:

account_type = "Legacy user"

account_type.lower()    #  "legacy user"
account_type.upper()    #  "LEGACY USER"
account_type.title()    #  "Legacy User"
account_type.swapcase() #  "lEGACY USER"

It’s also very easy (and safe) to convert other Python objects (such as numbers) into strings, to take advantage of the other methods that Python strings provide:

account_number = 123           # Note: this is a numeric value!

account_number_str = str(123)  # And this is the number, as text

account_number_str.isdigit()   # Returns True - only contains digits

account_number_str.isalpha()   # Returns False - doesn't contain letters

# Right-justify out to a length of 6, with zero characters:

account_number_str.rjust(6, "0")  # 123 becomes "000123"

Python also has a powerful “string interpolation” feature in the form of f-strings: Adding a lowercase f to the beginning of a string will allow you to embed other python variables and expressions into a string, by including the variable names and other Python code in {curly braces}:

first_name = "Jane"
last_name = "Smith"
full_name = f"{last_name}, {first_name}"

# full_name is now "Smith, Jane"

We can combine these features to accomplish some very complicated transformations in relatively little code:

account_number = record.get("AccountNumber")  # for example: 425

account_number = str(account_number).rjust(5, "0") # left-pad with zeroes

# account_number would now be: "00425"

item_code = record.get("ItemCode")  # for example: "dac"

record["sku"] = f"SKU-{account_number}-{item_code.upper()}"

# record["sku"] would now be: "SKU-00425-DAC"

To break this down:

  1. We extract some values from record:
    1. "AccountNumber", which is a digit like 425
    2. "ItemCode", which is text like "dac"
  2. We transform our values:
    1. Convert "AccountNumber" to text, from 425 to "425"
    2. Right-justify "AccountNumber" with zeroes until it is five characters long (from "425" to "00425"
  3. Create a new "sku" value that combines our data:
    1. The letters "SKU" and a dash "-"
    2. Then the padded account number text: "00425" and a dash "-"
    3. Then our item code, in upper-cased form: "dac" becomes "DAC"
  4. Our new record["sku"] field is now set to "SKU-00425-DAC"

This is just scratching the surface of what Python provides, but hopefully we've given you some examples how you can use it in some Transforms of your own.


Thanks for reading. Matterbeam transforms provide users with the control and flexibility to fully customize their data transformations. We're continuously working to expand functionality, so stay tuned for even more powerful Python-based data transformation features in the future!

Share This Post

Check out these related posts

Writing Transforms in Python - Part 4

Writing Transforms in Python - Part 3

Writing Transforms in Python - Part 2