Ch 8 - Strings Flashcards

1
Q

Think Julia

What kind of data is a string?

A

Strings are not like integers, floats, and Booleans. A string is a sequence, which means it is an ordered collection of other values. In this chapter you’ll see how to access the characters that make up a string, and you’ll learn about some of the string helper functions provided by Julia.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2741-2744). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Think Julia

How can you find out the number of characters in a string?

A

length is a built-in function that returns the number of characters in a string:

julia > fruits = “🍌 🍎 🍐”
“🍌 🍎 🍐”
julia > len = length( fruits) #including spaces
5

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2798-2801). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Think Julia

How can the number of bytes is a string be determined?

A

The function sizeof gives the number of bytes in a string:

julia > sizeof(“ 🍌”)
4

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2808-2812). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Think Julia

What is the term for processing a string one character at a time.

A

Traversal

A lot of computations involve processing a string one character at a time.

Often they start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. One way to write a traversal is with a while loop:

index = firstindex( fruits)
….while index < = sizeof( fruits)
……..letter = fruits[ index]
……..println( letter)
……..global index = nextind( fruits, index) end
end

This loop traverses the string and displays each letter on a line by itself. The loop condition is index < = sizeof( fruit), so when index is larger than the number of bytes in the string, the condition is false and the body of the loop doesn’t run.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2839-2841). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Think Julia

How do you slice a string?

A

String Slices A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

julia > str = “Julius Caesar”;
julia > str[ 1: 6]
“Julius”

A semicolon in REPL mode not only allows you to put multiple statements on one line but also hides the output.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2871-2885). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Think Julia

When using REPL, what does a semicolon at the end of the line do?

A

String Slices A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

julia > str = “Julius Caesar”;
julia > str[ 1: 6]
“Julius”

A semicolon in REPL mode not only allows you to put multiple statements on one line but also hides the output.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2871-2885). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Think Julia

Are string mutable?

A

**Strings Are Immutable **

It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example:

julia > greeting = “Hello, world!”
“Hello, world!”
julia > greeting[ 1] = ‘J’
ERROR: MethodError: no method matching setindex!(:: String, :: Char, :: Int64)

The reason for the error is that strings are immutable, which means you can’t change an existing string. The best you can do is create a new string that is a variation on the original:

julia > greeting = “J” * greeting[ 2: end]
“Jello, world!”

This example concatenates a new first letter onto a slice of greeting. It has no effect on the original string.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2900-2916). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Think Julia

How can a value from a variable be added to a string?

A

String Interpolation

Constructing strings using concatenation can become a bit cumbersome. To reduce the need for these verbose calls to string or repeated multiplications, Julia allows string interpolation using $:

julia > greet = “Hello”
“Hello”
julia > whom = “World”
“World”
julia > “$ greet, $( whom)!”
“Hello, World!”

This is more readable and convenient than string concatenation:

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2917-2929). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Think Julia

How can a string be made all upper case?

A

julia > uppercase(“ Hello, World!”)
“HELLO, WORLD!”

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2983-2985). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Think Julia

How can the location of a substring within a string be found?

A

julia > findfirst(“an”, “banana”)
2: 3

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 2992-2994). Kindle Edition.

~~~

```

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Think Julia

What does the ∈ operator do?

A

The ∈ Operator

The operator ∈ (\ in TAB) is a Boolean operator that takes a character and a string and returns true if the first appears in the second:

julia > ‘a’ ∈ “banana” # ‘a’ in “banana”
true

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3011-3013). Kindle Edition.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3004-3011). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Think Julia

What is an advantage of having well-chosen variable names?

A

With well-chosen variable names, Julia sometimes reads like English. You could read this loop as “for (each) letter in (the first) word, if (the) letter is an element of (the second) word, print (the) letter.”

Here’s what you get if you compare “apples” and “oranges”:

julia > inboth(“ apples”, “oranges”)
a e s

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3022-3027). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Think Julia

How can you find the characters two strings have in common?

A

With well-chosen variable names, Julia sometimes reads like English. You could read this loop as “for (each) letter in (the first) word, if (the) letter is an element of (the second) word, print (the) letter.”

Here’s what you get if you compare “apples” and “oranges”:

julia > inboth(“ apples”, “oranges”)
a e s

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3022-3027). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Think Julia

What is one way relational operators can be used with strings?

A

Other relational operations are useful for putting words in alphabetical order:

if word < “banana”
….println(“ Your word, $ word, comes before banana.”)
elseif word > “banana”
….println(“ Your word, $ word, comes after banana.”)
else
….println(“ All right, bananas.”)
end

Julia does not handle uppercase and lowercase letters the same way people do. All the uppercase letters come before all the lowercase letters, so:

Your word, Pineapple, comes before banana.

A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3035-3049). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Think Julia

In Julia, how are upper case and lower case letters ordered?

A

Other relational operations are useful for putting words in alphabetical order:

if word < “banana”
….println(“ Your word, $ word, comes before banana.”)
elseif word > “banana”
….println(“ Your word, $ word, comes after banana.”)
else
….println(“ All right, bananas.”)
end

Julia does not handle uppercase and lowercase letters the same way people do. All the uppercase letters come before all the lowercase letters, so:

Your word, Pineapple, comes before banana.

A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3035-3049). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly