1. Introduction to strings
So far, we have been working primarily with numerical data, but a lot of real world data is text rather than numbers.
2. Strings
Julia stores text data in strings. A string can be created using double quotation marks. You cannot use single quotation marks. There is no limit on the number of characters you can have in a string.
3. Triple quotes
If the piece of text is too long, then we can split it across multiple lines by using three double quotation marks. Everything between the triple quotes is part of the same string.
4. Triple quotes
Using three double quotation marks also allows us to have quotes within the string text. We can't do this using a single set of double quotes or it will cause an error.
5. Concatenating strings
We can stick two strings together using the star symbol. This is known as concatenating, and is a basic operation for processing text data.
6. String interpolation
One helpful feature of Julia is its string interpolation. In this example, we write the greeting string. In the location where we would like to insert the value of the 'name' variable, we write a dollar sign followed immediately by name. Julia inserts the value in this location.
7. String interpolation
We can use string interpolation to insert other items into the strings. We can insert numbers, including both integers and floats. We can also insert booleans, and characters.
8. String interpolation
If we use parentheses immediately after the dollar sign we can insert calculated results into the string. Here we insert the result of x times y into the string.
9. String interpolation
To avoid using string interpolation, and instead have a dollar symbol in the string text, we put a backslash in front of the dollar symbol. When we print it, no interpolation will be used.
10. Indexing strings
We can add things into strings, but we can also extract them. The simplest way is with string indexing which lets us extract single characters from strings. Say we have a string recording a customer's assigned theater seat. We can extract the string's first character by typing the variable name followed by the number one inside square brackets.
Whenever we index an element from the string, the returned value has character data type.
11. Indexing strings
In this example, if we want to extract the seat number, we can select the second element of the string by putting a two inside the brackets. We could then use string interpolation to print a message to the customer.
12. Indexing strings
Since the seat number is the last element of the string, we can also use Julia's end keyword instead of the number two. The end keyword indexes the last element of the string.
13. Indexing strings
We can also use the end keyword to index items backward from the end of a string. We can index the E using end-minus-one inside the square brackets since the E is the second last element of the string.
14. Slicing strings
To extract more than one character from a string, we can use string slicing. String slicing copies a section from the original string. In this example, we want to extract the time of purchase and price from this receipt data.
15. Slicing strings
The time is the first five characters of the string. To extract this substring, we write the variable name followed by square brackets containing one-colon-five. This notation selects all indexes from one up to and including five.
16. Slicing strings
The price is the last five characters of the string, and to extract it we can use the end keyword again. We slice from the index which is four before the last index up to the end.
17. Let's practice!
Let's practice this in the exercises.