A string is just a text like “Hello”, “123”, or “My name is John”. It can have letters, numbers, spaces, or symbols.
Mojo strings are used whenever you need to store or work with text, for example:
- A person’s name (“John”),
- An address (“221B Baker Street”),
- A message (“Welcome to Mojo”).
How To Create String in Mojo?
In Mojo, we can create a string by double quotes ( ” ) or single quotes ( ‘ ).
For example:
name = "Mojo Language"
city = 'New York'
Both single and double quotes work the same way, so you can choose one and stay consistent in your code for readability.
How To Create a Multi-line String in Mojo?
If you need a string that spans multiple lines, such as a paragraph or a long message, you can use triple quotes like this:
message = """Welcome to the Mojo workshop!
Here, you will learn how to code with speed and efficiency."""
print(message)
Triple quotes preserve line breaks automatically.
How To Index String in Mojo?
We can index a string in sequence, meaning each character has a position like starting from 0.
word = "Mojo"
print(word[0]) # M
print(word[3]) # o
Here, word[0] gives the first character ( M ), and word[3] gives the last character ( o ).
Mojo String Slicing Method
We can get some parts of a string using the slicing method.
For example: string[start:end]
text = "MojoLang"
print(text[0:4]) # Mojo
print(text[4:]) # Lang
- Text[0:4] → It will print characters from index 0 to 3.
- text[4:] → from index 4 till the end
What is The String Concatenation Method in Mojo?
Concatenation means joining two strings together. For example:
first = "Hello"
second = "Mojo"
result = first + " " + second
print(result) # Hello Mojo
This method is useful when you want to build messages dynamically.
String Repetition in Mojo
We can also repeat a string multiple times using the * operator.
laugh = "Hey! "
print(laugh * 3) # Hey! Hey! Hey!
How To Check String Length in Mojo?
We can use the len() function to find out how many characters are available in a string.
text = "Mojo"
print(len(text)) # 4
Important String Methods in Mojo
Mojo supports useful built-in methods for strings:
data = " hello mojo "
print(data.upper()) # HELLO MOJO
print(data.lower()) # hello mojo
print(data.strip()) # hello mojo (removes spaces)
print(data.replace("mojo", "world")) # hello world
print(data.startswith(" he")) # True
print(data.endswith("jo ")) # True
These methods help in string cleaning, searching, and formatting.
How To Search a Specific String in Mojo?
We can check if a specific string exists or not in the text. Mojo provides in keyword to check a string. For example:
sentence = "Learning Mojo is fun"
print("Mojo" in sentence) # True
print("Python" in sentence) # False
String Formatting in Mojo
We can format strings for cleaner code, instead of manually concatenating values. For example:
name = "Ravi"
score = 95
message = f"Hello {name}, your score is {score}."
print(message)
In this code, f-strings make it easy to insert variables directly into a string.
How To Escaping Characters?
We can easily include quotes inside strings using the escape character \.
quote = "He said, \"Mojo is amazing!\""
print(quote)
Strings are Immutable in Mojo
Once we create a string, it cannot be changed directly. When we modify a string, we create a new string.
text = "Mojo"
text = text + "Lang"
print(text) # MojoLang
Real-Life Example: Student Report Card
Let’s create a simple program using the strings in Mojo:
fn generate_report(student_name: str, subject: str, marks: Int) -> str:
status = "Pass" if marks >= 40 else "Fail"
report = f"Student: {student_name}\nSubject: {subject}\nMarks: {marks}\nStatus: {status}"
return report
# Example usage
result = generate_report("Peter", "Math", 85)
print(result)
Output:
Student: Peter
Subject: Math
Marks: 85
Status: Pass
This example combines string formatting, newlines, and conditions, like a real-life scenario for students.
Student Exercise: “Secret Code Name Generator”
You are helping a detective agency to create secret code names for agents.
The code name is generated by:
- Taking the agent’s full name.
- Removing extra spaces from the start and end.
- Extracting the first three letters of the first name.
- Extracting the last two letters of the last name.
- Combining them and converting everything to UPPERCASE.
- Showing the final code name in a message.
Example Input & Output:
If you enter the name, it should print the code name like below:
Input:
Enter full name: Priya Sharma
Output:
Original Name: Priya Sharma
Secret Code Name: PRIMA
Try this exercise by yourself and boost your Mojo programming skills on String methods.