What are strings in Julia?

Overview

A string is a finite sequence of one or more characters, enclosed in double-quotes. An example of a string is:

“here is a string between double quotes”

The important things to note about strings are:

  • Strings are immutable, i.e., they cannot be changed once they are created.
  • Strings can be assigned to variables and passed around in the code.

Code

greeting = "Hello I am a Julia string"
intro = greeting
println(intro)

Output

Hello I am a Julia string

In Julia, elements of a string can be extracted to form multiple substrings using the square bracket.

Code

string1 = "extract me"
element = string1[1:3]
println(element)

Output

ex

Strings can also be interpolated in Julia. Interpolation is the process of executing whatever is executable in a string. In Julia, a dollar sign () is used to insert the value of a variable in the string.

Code

name = "John"
age = 47
println("Hello $name, your age is $age")

Output

Hello John, your age is 47

The built-in concrete type used for strings and string literals in Julia is String. This supports a full range of Unicode characters via the UTF-8 encoding.

Free Resources