Tutorials
Blog
About

Tutorials > Julia Programming: a Hands-on Tutorial

Using Strings and Numbers

by Martin D. Maas, Ph.D
@MartinDMaas

Last updated: 2021-10-25

Julia has excellent support for strings. In this section we’ll just go about the basics of defining, concatenating strings, and converting numbers to strings.

As usual, if you are looking for a full reference and advanced topics, check out the official documentation, in this case the Strings reference.

Strings and Chars

One basic distinction is that Julia contains two relevant data types: string and char. Strings are initialized with " while chars use '.

"Hello"             # string
'H'                 # char
'Hello'             # error!! character literal contains multiple characters

Concatenate Strings

There are at least two ways to concatenate strings in Julia: you can use the * operator and the string function.

"Hello " * "wor" * "ld"
string("Hello", " ", "world")

both options will return the string "Hello world"

Convert Number to String

There are various ways of converting a number to a string:

Let’s see what we obtain if we apply this to the number 1/7:

string(1/7)               # Returns "0.14285714285714285".
"$(1/7)"                  # Returns "0.14285714285714285".
`@sprintf("%6.4f",1/7)`   # Returns `"0.1429"`

Concatenate a number to a string

Now let’s check out a few examples of both string concatenation, and number-to-string conversion.

Using the string function

The simplest way is to use the string function

string("In ancient Babylon they approximated pi to 25/8, which is ", 25/8)
"In ancient Babylon they approximated pi to 25/8, which is 3.125"

Using string interpolation

We can obtain the same result using “string interpolation”.

"In ancient Babylon they approximated pi to 25/8, which is $(25/8)"

With string interpolation, the expression between parenthesis we’ll be evaluated (which in this case returns a Float), and then converted to a string.

As you can see, this notation is more compact than the string function, while it’s still easy to read. It can be convenient when you need to do many concatenations, like when doing something like:

rmse = 1.5; mse = 1.1; R2 = 0.94
"Our model has a R^2 of $(R2), rmse of $(rmse), and mse of $(mse)"
"Our model has a R^2 of 0.94, rmse of 1.5, and mse of 1.1"

Using the Printf package

In case you need to control the number of digits in the output, or use scientific notation, you can use the classic, C-style, Printf package, which contains the @sprintf macro that returns a string.

using Printf
str = @sprintf("Archimedes approximated pi to 22/7, which is %.4f...", 22/7)
"Archimedes approximated pi to 22/7, which is 3.1429..."

The package follows the C convention. Even though this can be hard to read and use, it is very powerful when we need precise control on the final format.

The cases which I tend to use most frequently are:

format description example
%.4f 4 decimal digits 100.1429
%.4e Scientific notation with 4 digits 1.4286e-05

You can check out more examples in the Julia documentation, or the full specification of the format in the C++ documentation

Ask me a question or send me your comments!

Don't hesitate to ask me any question about the topics I cover on this blog!

Click here to reach out!