How to use strings in Julia

Strings are what normal people call “text”. Here’s how to do all sorts of cool stuff in Julia with strings.

Ron Erdos
Updated April 13, 2025
Tested with Julia version 1.11.4

What are strings?

The simplest way to think about strings in programming—including in Julia—is as text.

The following are all strings:

"The quick brown fox jumped over the lazy dog."

"hello world"

"There are 423 items in the warehouse."

What is an example of a non-string?

An example of something that isn’t a string would be an integer—for example, 5.

You can perform mathematical operations with integers e.g.

5 + 5

We get:

10

Or if we input:

5 * 5

… we get:

25

Anyway, back to strings—read on for more.

Double quotes, not single quotes for strings in Julia

In programming languages, strings need to be surrounded by quotes.

In Julia, we need to use double quotes, not single quotes.

If you use single quotes, like this:

'hello world'

… you’ll get an error, which reads, in part:

ERROR: ParseError:
# Error @ REPL[19]:1:2
'hello world'
#└─────────┘ ── character literal contains multiple characters
Stacktrace:
 [1] top-level scope
   @ none:1

But if you use double quotes, like this:

"hello world"

… all is well—we can see the REPL outputs our string without issue:

"hello world"

Happy days.

How insert variables into strings in Julia (string interpolation)

Let’s say you had a warehouse with a constantly changing number of items in it.

And let’s say you wanted to output a sentence describing this.

Something like this:

"The number of items in the warehouse is 423"

… but where the numeral changes based on the actual count.

Let’s say we actually had 995 items in the warehouse.

Here’s how we can do this in Julia:

item_count = 995

println("The number of items in the warehouse is $item_count")

We get:

"The number of items in the warehouse is 995"

Julia makes this sort of thing very easy.

First, we create a variable called item_count. This will store the number of items in the warehouse.

Next, we simply include our variable—item_count in this case—inside our string but with a dollar sign $ at the front to tell Julia to treat it as a variable.

What if we didn’t use the dollar sign?

If we didn’t use the dollar sign, like so:

item_count = 995

println("The number of items in the warehouse is item_count")

… then Julia would treat item_count as literal text—called a string literal—instead of a variable:

"The number of items in the warehouse is item_count"

… which is obviously not what we want.

What if you need to use a dollar sign e.g. to show currency?

If you want to print something like the following:

"The price is $15"

… then you need to “escape” the dollar sign so Julia treats it literally rather than expecting a variable.

If we just did this:

println("The price is $15")

… then we get an error:

identifier or parenthesized expression expected after $ in string

Here’s how we can use dollar signs literally in Julia:

println("The price is \$15")

We get:

"The price is $15"

How to use variables in strings in Julia (string interpolation)

Above, we covered an example where we had a string with the value "hello world 5 + 5".

We saw that our output would be unchanged, i.e. "hello world 5 + 5".

But if we wanted our output to be "hello world 10", how could we do this?

With variables, and something called “string interpolation”.

Let me explain with an example:

my_number = 5

println("hello world")

How to concatenate two or more strings in Julia?

There are a couple of ways to concatenate strings in Julia.

The way I use most often is this:

"foo" * "bar"

We get …

"foobar"

How to check if a string contains non-ASCII characters in Julia?

Let’s say you have a bunch of strings that contain English words and standard punctuation (which are both ASCII) and Traditional Chinese words (which are not).

Some strings have both English and Traditional Chinese, like this:

"Which way to 北京?"

Whereas some strings are just in English, like this:

"Which way to Sydney, Australia?"

To check if any characters in the string are non-ASCII, we can use the all() function and isascii to check.

Let’s test the first string:

all(isascii, "Which way to 北京?")

We get:

false

And let’s test the second string with the same logic:

all(isascii, "Which way to Sydney, Australia?")

We get:

true

So from the above, we can say that the first string has non-ASCII characters—in our case, Traditional Chinese—whereas the second string is all ASCII, which means, by definition, it won’t have any Traditional Chinese characters.

Caveat: Obviously there are many characters which aren’t ASCII which are also not Traditional Chinese.

For example, Thai script—such as เชียงราย—is also not ASCII. However, if you’re working with a known, limited dataset, the above code can be very useful.

How to check if a string contains a numeral?

Let’s say you had the following string: "2 brown foxes jumped over the lazy dog 9 times."

And let’s say you wanted to check if there was a numeral (digit) in this string.

We can do this with a built-in Julia function called isdigit().

Now, this function only works character-by-character. So if we try to do this:

isdigit("2 brown foxes jumped over the lazy dog 9 times.")

… we get the following error:

ERROR: MethodError: no method matching isdigit(::String)

Instead, we need to use the function any() and isdigit, like this:

any(isdigit, "2 brown foxes jumped over the lazy dog 9 times.")

We get:

true

So we know our foxes-and-dog string does in fact contain digits.

If you wanted Julia to print out the numerals from the string, we could do something like this:

s = "2 brown foxes jumped over the lazy dog 9 times."
# ☝️ We have turned our string into a variable

for char in s # iterate through each character in our string
    if isdigit(char) # if the character is a numeral ...
        println(char) # ... print it to the terminal
    else # otherwise
        # do nothing
    end
end

We get:

2
9

… which is what we’d expect, as those were the numerals in our string.

Get Julia tips in your inbox a few times per year. Unsubscribe anytime.