You can use it to ensure there is a fixed number of characters in front of your text, like:
Original Message:
Sent: 12-10-2025 03:44
From: Peter Møller
Subject: MapInfo Monday: String Manipulation with Python
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)) + txtprint(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
Original Message:
Sent: 12-09-2025 21:33
From: Nick Lawrence
Subject: MapInfo Monday: String Manipulation with Python
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
Original Message:
Sent: 03-17-2025 04:00
From: Peter Møller
Subject: MapInfo Monday: String Manipulation with Python
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
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 already have started using Python which part of you found really useful or powerful?
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------