MapInfo Pro

 View Only

MapInfo Monday: String Manipulation with Python

  • 1.  MapInfo Monday: String Manipulation with Python

    Employee
    Posted 03-17-2025 04:00

    In this short #MapInfoMonday article, I wanted to give you a quick overview of how to manipulate your strings in Python. Python string support is a bit more capable than string support in MapBasic.

    It's not that the MapBasic string support is really bad. You just realize that a few nice string methods are missing. Maybe you already have written custom functions to make up for the lack of some of these.

    Similar to MapBasic

    Python does allow you to change the case of your string.

    A few examples:

    • .lower(): The MapBasic language uses LCase$() to do the same.
    • .title(): Proper() is a similar MapBasic function. It changes the string so that the first character in every word is uppercase and the rest of the characters are lowercase.
    • .upper(): This is similar to the MapBasic function UCase$(). They will both convert a string to all uppercase.

    Below you can see an example of how to convert a string to uppercase using Python and MapBasic.

    Some methods for searching and replacing within a string
    • .find(): Returns the index where the substring was found within the string. This is similar to the MapBasic function InStr().
    • .strip(): This is similar to using RTrim$(LTrim$()) in MapBasic which will remove empty/whitespace characters from the start and end of the string.

    The Additional Python Functions

    Python has a few additional methods to work with cases:

    • .capitalize(): This makes the first character uppercase and the rest lowercase.
    • .islower(): Does the string contain all lowercase characters?
    • .istitle(): Does the string contain all title case characters?
    • .isupper(): Does the string contain all uppercase characters?
    • .swapcase(): Changes the case of all the characters to the uppersite.

    Python comes with a long list of methods to help you find and replace characters in a string:

    • .count(): Counts the number of times a substring is found within a string
    • .endswith(): Checks if the string ends with the specified substring. This can be done using the MapBasic function Right$() to extract a part of the string and then compare this to the substring. Or you can use the Like operator: s Like "%" + substring. Far from the simplicity of the Python method
    • .removeprefix(): If the string starts with the substring, return the string without the prefix.
    • .removesuffix(): If the string ends with the substring, return the string without the postfix.
    • .replace(): Replaces all instances of the substring in the string with the new substring
    • .rfind(): Similar to find but will return the last index where the substring was found.
    • .startswith(): Checks if the string starts with the specified substring. This can be done using the MapBasic function Left$() to extract a part of the string and then compare this to the substring. Or you can use the Like operator: s Like substring + "%". Far from the simplicity of the Python method.

    You can also use MapBasic to do a simple replace. If you know that the substring you are looking for is at the end of the string. You can see an example of this in the image below.

    If you need to look for the string inside the string and even for multiple instances, you will have to create a custom function in MapBasic. You can find one in the STRINGLib on GitHub: STRINGReplace.
    If you already have started using Python which part of you found really useful or powerful?
    You can find more details and additional Python String Methods in the Python Documentation.


    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------