Mojo String Basics
A string is a sequence of characters stored as a single value. Names, sentences, file paths, and messages are all strings. Mojo's String type provides a rich set of operations for creating, inspecting, and transforming text.
Creating Strings
fn main():
var greeting: String = "Hello, Mojo!"
var empty: String = ""
var number_as_text: String = "42"
print(greeting)
print(len(empty)) # 0
print(number_as_text)
String literals always use double quotes in Mojo. The characters inside are stored in memory as a contiguous sequence of bytes.
Multi-line Strings
fn main():
var poem = """
Roses are red,
Mojo is fast,
Python is friendly,
Both are a blast.
"""
print(poem)
Triple double-quotes let you span a string across multiple lines without inserting special characters manually.
String Length
fn main():
var word = "Compiler"
print(len(word)) # 8
C o m p i l e r
0 1 2 3 4 5 6 7 ← index positions
len("Compiler") = 8
Accessing Characters
Each character in a string sits at a numbered position called an index. Mojo uses zero-based indexing — the first character is at index 0.
fn main():
var city = "Tokyo"
print(city[0]) # T
print(city[1]) # o
print(city[4]) # o
T o k y o
0 1 2 3 4
↑ ← city[0] = 'T'
↑ ← city[3] = 'y'
String Concatenation
Concatenation joins two strings end to end using the + operator.
fn main():
var first = "Mojo"
var second = " is fast"
var sentence = first + second
print(sentence) # Mojo is fast
"Mojo" + " is fast"
M o j o i s f a s t
└─────┘ └───────────┘
first second
└──────────────────────┘
sentence
You cannot concatenate a string with a number directly. Convert the number to a string first using String().
fn main():
var score = 95
var message = "Your score: " + String(score)
print(message) # Your score: 95
String Repetition
The * operator repeats a string a given number of times.
fn main():
var line = "-" * 20
print(line) # --------------------
var cheer = "Go! " * 3
print(cheer) # Go! Go! Go!
Useful String Methods
upper() and lower()
fn main():
var text = "Hello Mojo"
print(text.upper()) # HELLO MOJO
print(text.lower()) # hello mojo
strip()
Removes leading and trailing whitespace (spaces, tabs, newlines).
fn main():
var messy = " lots of space "
print(messy.strip()) # "lots of space"
replace()
fn main():
var sentence = "I love Python"
var updated = sentence.replace("Python", "Mojo")
print(updated) # I love Mojo
find()
Returns the index of the first occurrence of a substring. Returns -1 if not found.
fn main():
var text = "Mojo is great"
print(text.find("great")) # 8
print(text.find("slow")) # -1
M o j o i s g r e a t
0 1 2 3 4 5 6 7 8 9 10 11 12
↑
"great" starts at index 8
startswith() and endswith()
fn main():
var filename = "report_2025.pdf"
print(filename.startswith("report")) # True
print(filename.endswith(".pdf")) # True
print(filename.endswith(".docx")) # False
String Slicing
Extract a portion of a string by specifying a start index and an end index. The slice includes the start position but excludes the end position.
fn main():
var message = "Hello, Mojo!"
# Extract "Mojo"
var sub = message[7:11]
print(sub) # Mojo
H e l l o , M o j o !
0 1 2 3 4 5 6 7 8 9 10 11
message[7:11]
↑──────↑
7 11 (excluded)
Result: M o j o
Checking String Contents
fn main():
var code = "mojo123"
print(code.isdigit()) # False — has letters
print("999".isdigit()) # True — all digits
print(code.isalpha()) # False — has digits
print("Mojo".isalpha()) # True — all letters
Escape Characters
Some characters need a special two-character code called an escape sequence to appear inside a string.
Escape | Meaning -------|------------------ \n | New line \t | Tab (horizontal) \\ | Backslash itself \" | Double quote
fn main():
print("Line 1\nLine 2")
print("Name:\tMojo")
print("Path: C:\\Users\\Mojo")
print("She said, \"Mojo rocks!\"")
Output:
Line 1 Line 2 Name: Mojo Path: C:\Users\Mojo She said, "Mojo rocks!"
Strings Are Immutable
You cannot change a single character inside a string after creating it. Every string operation creates a new string object rather than modifying the original.
Immutable string model:
original = "Mojo"
modified = original.replace("M", "B")
original → "Mojo" (unchanged)
modified → "Bojo" (new string)
Key Takeaways
Strings store sequences of characters enclosed in double quotes. Use triple quotes for multi-line strings. Access individual characters by index starting at zero. The + operator concatenates strings and * repeats them. Methods like upper(), strip(), replace(), and find() transform and search strings. Slicing extracts substrings with start and end indices. Strings are immutable — every change produces a new string object.
