Introduction to strings

Introducción a 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, ...
Introducción a 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!
Introducción a Julia

Triple quotes

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

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


ERROR: syntax: ...
Introducción a Julia

Concatenating strings

name = "James"

greeting = "Well hello there, "


# Concatenate two strings
println(greeting*name)
Well hello there, James
Introducción a Julia

String interpolation

name = "James"


# Interpolate with $ symbol greeting = "Well hello there, $name"
println(greeting)
Well hello there, James
Introducción a 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
Introducción a 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
Introducción a 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)
Introducción a Julia

Indexing strings

# Customer's seat
seat = "E5"


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

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

Char
Introducción a 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.
Introducción a 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.
Introducción a 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.
Introducción a Julia

Slicing strings




receipt = "08:30 - coffee - \$3.50" println(receipt)
08:30 - coffee - $3.50
Introducción a Julia

Slicing strings

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


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

Introducción a 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
Introducción a Julia

Let's practice!

Introducción a Julia

Preparing Video For Download...