VBAtoPython

VBA String Functions to Python

Complete mapping for Trim, UCase, LCase, Left, Right, Mid, InStr, Replace, and Len — with code examples, edge cases, and the 1-based indexing gotcha.

Quick Reference

VBAPythonIndexing
Trim(s)s.strip()N/A
UCase(s)s.upper()N/A
LCase(s)s.lower()N/A
Left(s, n)s[:n]Same
Right(s, n)s[-n:]Same
Mid(s, start, len)s[start-1:start-1+len]VBA is 1-based
InStr(s, find)s.find(find) + 1VBA is 1-based
Replace(s, old, new)s.replace(old, new)N/A
Len(s)len(s)Same

The 1-Based Indexing Gotcha

VBA string functions like Mid and InStr use 1-based positions. Python uses 0-based indexing. This is the most common source of off-by-one bugs during migration.

VBA (1-based)

Mid("ABCDE", 2, 3)
' Returns "BCD"
' Position 2 = "B"

Python (0-based)

"ABCDE"[1:1+3]
# Returns "BCD"
# Index 1 = "B"

The converter generates helper functions (vba_mid, vba_instr) that handle this adjustment automatically so the Python output behaves identically to VBA.

Trim

VBA

result = Trim("  hello  ")
' Returns "hello"

Python

result = "  hello  ".strip()
# Returns "hello"

VBA Trim only removes spaces. Python strip() removes all whitespace (tabs, newlines). The converter generates a vba_trim() helper that matches VBA behavior.

UCase / LCase

VBA

upper = UCase("hello")   ' "HELLO"
lower = LCase("HELLO")   ' "hello"

Python

upper = "hello".upper()   # "HELLO"
lower = "HELLO".lower()   # "hello"

Direct mapping. The converter generates helper wrappers that call str() first to handle non-string inputs safely.

Left

VBA

result = Left("Hello World", 5)
' Returns "Hello"

Python

result = "Hello World"[:5]
# Returns "Hello"

Python slicing is 0-based but the behavior is identical for Left since it always starts from position 0.

Right

VBA

result = Right("Hello World", 5)
' Returns "World"

Python

result = "Hello World"[-5:]
# Returns "World"

Python negative indexing makes this natural. Edge case: Right(s, 0) returns empty string in both.

Mid

VBA

result = Mid("Hello World", 7, 5)
' Returns "World"

result = Mid("Hello World", 7)
' Returns "World" (to end)

Python

result = "Hello World"[6:6+5]
# Returns "World"

result = "Hello World"[6:]
# Returns "World" (to end)

Critical difference: VBA Mid is 1-based, Python slicing is 0-based. The converter subtracts 1 from the start position automatically.

InStr

VBA

pos = InStr("Hello World", "World")
' Returns 7 (1-based)

pos = InStr("Hello", "xyz")
' Returns 0 (not found)

Python

pos = "Hello World".find("World") + 1
# Returns 7 (adjusted to 1-based)

pos = "Hello".find("xyz") + 1
# Returns 0 (not found)

VBA InStr returns 1-based position (0 = not found). Python find() returns 0-based position (-1 = not found). The helper adds 1 to match VBA behavior.

Replace

VBA

result = Replace("a-b-c", "-", "_")
' Returns "a_b_c"

Python

result = "a-b-c".replace("-", "_")
# Returns "a_b_c"

Direct mapping. Both replace all occurrences by default.

Len

VBA

n = Len("Hello")
' Returns 5

Python

n = len("Hello")
# Returns 5

Direct mapping. No helper needed.

String Concatenation

VBA

result = "Hello" & " " & name & vbCrLf

Python

result = "Hello" + " " + name + "\n"

The converter maps & to + and replaces VBA constants like vbCrLf with Python escape sequences.

Related Guides

See these helpers in action

Paste a VBA macro that uses string functions and see the Python helpers generated automatically.