Introduction to strings

Introduction to Julia

James Fulton

Climate informatics researcher

Strings

# Strings are surrounded by " "
name = "Jane" # Cannot use 'Jane'


# Strings can be any length book = "It is a truth universally acknowledged, ..."
println(name)
println(book)
Jane

It is a truth universally acknowledged, ...
Introduction to Julia

Triple quotes

# Triple quotes 
poem = """Beware the Jabberwock, my son!

      The jaws that bite, the claws that catch!"""


println(poem)
Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!
Introduction to Julia

Triple quotes

greeting = """ " Well hello there " """

println(greeting)
 " Well hello there " 
greeting = " " Well hello there " "


ERROR: syntax: ...
Introduction to Julia

Concatenating strings

name = "James"

greeting = "Well hello there, "


# Concatenate two strings
println(greeting*name)
Well hello there, James
Introduction to Julia

String interpolation

name = "James"


# Interpolate with $ symbol greeting = "Well hello there, $name"
println(greeting)
Well hello there, James
Introduction to Julia

String interpolation

x_int = 10


# Insert integer into string println("The value is $x_int")
The value is 10
x_float = 1.0


# Insert float into string println("The value is $x_float")
The value is 1.0
x_bool = true


# Insert boolean into string println("The value is $x_bool")
The value is true
x_char = 'A'


# Insert character into string println("The value is $x_char")
The value is A
Introduction to Julia

String interpolation

x = 10
y = 3


# Insert x*y into string println("The product of x and y is $(x*y)")
The product of x and y is 30
Introduction to Julia

String interpolation

x = 10
y = 3


# Insert x*y into string println("The product of x and y is \$(x*y)")
The product of x and y is $(x*y)
Introduction to Julia

Indexing strings

# Customer's seat
seat = "E5"


# Select character row = seat[1] # this returns 'E'

println(row)
println(typeof(row))
E

Char
Introduction to Julia

Indexing strings

# Customer's seat
seat = "E5"

# Select characters
row = seat[1]       # this returns 'E'
number = seat[2]    # this returns '5'


println("Your seat is in row $row, seat number $number.")
Your seat is in row E, seat number 5.
Introduction to Julia

Indexing strings

# Customer's seat
seat = "E5"

# Select characters
row = seat[1]       # this returns 'E'
number = seat[end]  # this returns '5'

println("Your seat is in row $row, seat number $number.")
Your seat is in row E, seat number 5.
Introduction to Julia

Indexing strings

# Customer's seat
seat = "E5"

# Select characters
row = seat[end-1]   # this returns 'E'
number = seat[end]  # this returns '5'

println("Your seat is in row $row, seat number $number.")
Your seat is in row E, seat number 5.
Introduction to Julia

Slicing strings




receipt = "08:30 - coffee - \$3.50" println(receipt)
08:30 - coffee - $3.50
Introduction to Julia

Slicing strings

# Index position:
#          12345...
receipt = "08:30 - coffee - \$3.50"


time = receipt[1:5] # Select first 5 characters
println(time)
08:30

Introduction to Julia

Slicing strings

# Index position from end:
#                            4321end
receipt = "08:30 - coffee - \$3.50"

time = receipt[1:5]         # Select first 5 characters

price = receipt[end-4:end] # Select last 5 characters
println(time)
println(price)
08:30

$3.50
Introduction to Julia

Let's practice!

Introduction to Julia

Preparing Video For Download...