MapInfo Pro

 View Only
  • 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
    ------------------------------


  • 2.  RE: MapInfo Monday: String Manipulation with Python

    Posted 12-09-2025 21:34

    Is there a function to pad a string with whitespace to achieve a string of a specified length?

    For example, pad with whitespace on the right so that the string is now 20 characters wide?



    ------------------------------
    Nick Lawrence
    Senior Spatial Science Officer
    Department of Transport and Main Roads (QLD)
    Brisbane QLD
    ------------------------------



  • 3.  RE: MapInfo Monday: String Manipulation with Python

    Employee
    Posted 12-10-2025 03:44
    Edited by Peter Møller 12-12-2025 05:00

    Hey Nick

    With Python, you can use the function rjust on your string:

    txt = "banana"
    x = txt.rjust(20, " ")
    print(x)

    Read more on W3Schools. Python also has a ljust and cjust functions.

    MapBasic, on the other hand, has the functions Space$() and String$(). They can achieve the same result but require just a bit more work.

    txt = "banana"
    x = Space$(20 - Len(txt)) + txt
    print x

    As you can see above, you need to subtract the current length of the string from the length you are looking for when using the Space$() function, and then concatenate this with your existing string.

    String$() is more versatile than Space$() as it can use any string when it builds out the string.

    txt = "banana"
    x = String$(20 - Len(txt), "-")
    print x

    Keep in mind, it will only use the first character of the input string if it is longer than 1 character.

    I hope this helps



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



  • 4.  RE: MapInfo Monday: String Manipulation with Python

    Employee
    Posted 12-12-2025 05:00

    I had forgotten about Format$(), which is quite powerful.

    You can use it to ensure there is a fixed number of characters in front of your text, like:

    x = 1
    txt = Format$(x, "0000")
    Print txt


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