Julia Strings
A String is a finite sequence of characters.
In this tutorial, we will learn how to initialize a String and some of the basic operations with Strings like concatenation and interpolation.
Julia strings can contain letters, digits, spaces, punctuation, and Unicode characters. Julia represents a string with the String type, while an individual character is represented with the Char type.
Initialization
String literals are defined with the string in double quotes " " or triple double quotes """ """.
script.jl
s1 = "I am a string."
println(s1)
s2 = """I am also a string."""
println(s2)
Output
I am a string.
I am also a string.
Julia string literals with double and triple quotes
Double quotes are normally used for short string literals. Triple double quotes are useful when a string spans multiple lines or contains double-quote characters that would otherwise need escaping.
single_line = "Julia string"
multiple_lines = """
This string contains
more than one line.
"""
println(single_line)
println(multiple_lines)
Julia string
This string contains
more than one line.
Julia String and Char values
A string uses double quotes, whereas a single character uses single quotes. These values have different Julia types.
text = "A"
letter = 'A'
println(typeof(text))
println(typeof(letter))
String
Char
Concatenate Strings
You can concatenate two or more Strings in Julia using string(str1, str2, ...) function.
In this example, we will concatenate two strings.
script.jl
s1 = "There are seven continents";
s2 = " and five oceans.";
s3 = string(s1, s2)
println(s3)
Output
There are seven continents and five oceans.
In this example, we take four strings and concatenate them in a single statement.
script.jl
s1 = "There are seven continents";
s2 = " and five oceans.";
s3 = " This is third string.";
s4 = " This is fourth.";
resultStr = string(s1, s2, s3, s4)
println(resultStr)
Output
There are seven continents and five oceans. This is third string. This is fourth.
Julia string concatenation with string, *, and repeat
Besides the string() function, Julia supports string concatenation with the multiplication operator *. The exponentiation operator ^ can repeat a string a specified number of times.
first = "Julia"
second = " strings"
combined = first * second
repeated = "ha" ^ 3
println(combined)
println(repeated)
Julia strings
hahaha
Use string() when combining values of different types. Julia converts the supplied values to their string representations before joining them.
name = "Maya"
age = 28
message = string(name, " is ", age, " years old.")
println(message)
Maya is 28 years old.
String Interpolation
Dollar sign $ can be used to insert existing variables into a string literal and evaluate expressions within the string itself.
script.jl
a = 7
b = 5
str = "a is $a. b is $b. a*b is $(a*b)";
println(str)
Output
a is 7. b is 5. a*b is 35
In this example, we have done an arithmetic operation inside the string using string interpolation.
Julia string interpolation with variables and expressions
Use $variable to insert a simple variable. Enclose a complete expression in parentheses, as in $(expression), when calculation or property access is required.
product = "Notebook"
price = 4.50
quantity = 3
println("Product: $product")
println("Total: $(price * quantity)")
Product: Notebook
Total: 13.5
Escape sequences in Julia strings
A backslash introduces an escape sequence inside a Julia string. Common escape sequences include \n for a new line, \t for a tab, \" for a double quote, and \\ for a backslash.
message = "Name:\tJulia\nType:\tLanguage"
quote = "She said, \"Hello.\""
path = "C:\\projects\\julia"
println(message)
println(quote)
println(path)
Name: Julia
Type: Language
She said, "Hello."
C:\projects\julia
Accessing characters and substrings in Julia
Julia uses one-based indexing. The first valid string index is available through firstindex(), and the final valid index is returned by lastindex().
language = "Julia"
println(language[firstindex(language)])
println(language[lastindex(language)])
println(language[1:3])
J
a
Jul
For strings containing multibyte Unicode characters, not every integer is necessarily a valid index. Functions such as firstindex(), lastindex(), nextind(), and prevind() help move between valid character boundaries.
Finding Julia string length and character count
The length() function returns the number of characters in a string. The ncodeunits() function returns the number of code units used by the string’s encoding, which can differ from the character count for Unicode text.
plain = "Julia"
unicode_text = "café"
println(length(plain))
println(length(unicode_text))
println(ncodeunits(unicode_text))
5
4
5
Splitting and joining strings in Julia
Use split() to divide a string into substrings. Use join() to combine a collection of values with a separator.
line = "red,green,blue"
colors = split(line, ",")
println(colors)
println(join(colors, " | "))
SubString{String}["red", "green", "blue"]
red | green | blue
Replacing text inside a Julia string
The replace() function returns a new string in which matching text has been substituted. Julia strings are immutable, so the original string is not modified.
sentence = "Julia is fast and Julia is expressive."
updated = replace(sentence, "Julia" => "The language")
println(sentence)
println(updated)
Julia is fast and Julia is expressive.
The language is fast and The language is expressive.
Converting between Julia strings and numbers
Use parse() when a string contains a numeric value that must be converted to a number. Use string() to convert a number or another value to its textual representation.
integer_text = "42"
decimal_text = "19.75"
number = parse(Int, integer_text)
price = parse(Float64, decimal_text)
number_as_text = string(number)
println(number)
println(price)
println(typeof(number_as_text))
42
19.75
String
If the input may not contain a valid number, use tryparse(). It returns nothing instead of raising an error when conversion fails.
valid_value = tryparse(Int, "100")
invalid_value = tryparse(Int, "one hundred")
println(valid_value)
println(invalid_value)
100
nothing
Useful Julia string functions
Julia provides functions for testing, searching, trimming, and changing the case of strings.
text = " Julia Programming "
println(strip(text))
println(lowercase(text))
println(uppercase(text))
println(startswith(strip(text), "Julia"))
println(endswith(strip(text), "Programming"))
println(occursin("Program", text))
Julia Programming
julia programming
JULIA PROGRAMMING
true
true
true
Common mistakes when working with Julia strings
- Using single quotes for a string: Single quotes create a
Char, not aString. - Using zero-based indexing: Julia string indexing starts from one, not zero.
- Assuming every integer is a valid Unicode index: Multibyte characters can make some integer positions invalid.
- Trying to modify a character in place: Julia strings are immutable; create a new string instead.
- Using
parse()on unchecked input: Usetryparse()when invalid input is possible.
Frequently asked questions about Julia strings
How do you concatenate strings in Julia?
Use string(value1, value2) or the * operator. For example, "Julia" * " language" returns "Julia language".
How does Julia string interpolation work?
Place $ before a variable name, such as "Hello, $name". Use parentheses for an expression, such as "Total: $(price * quantity)".
How do you convert a Julia string to an integer?
Use parse(Int, text) when the text is known to contain a valid integer. Use tryparse(Int, text) when invalid input should return nothing instead of an exception.
How do you split a string in Julia?
Call split(string, delimiter). For example, split("a,b,c", ",") returns substrings for a, b, and c.
Are Julia strings mutable?
No. Julia strings are immutable. Operations such as concatenation and replacement return new string values rather than modifying the existing string.
Conclusion
In this Julia Tutorial, we learned about Strings in Julia, how to initialize a string, interpolate a string, etc.
Julia provides direct syntax and built-in functions for creating, combining, searching, splitting, replacing, and converting strings. When processing Unicode text, use Julia’s string-indexing functions instead of assuming that every integer position identifies a character.
Editorial QA checklist for Julia string examples
- Confirm strings use double quotes and individual
Charvalues use single quotes. - Verify every interpolation example uses
$variableor$(expression)correctly. - Check that concatenation examples distinguish
string(),*, and string repetition with^. - Test substring examples with one-based indexing and review Unicode indexing claims.
- Run all
split(),replace(),parse(), andtryparse()examples and confirm their displayed outputs.
TutorialKart.com