Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sometimes, we need characters in a string that are hard to represent in program code.
Escape sequences are used to represent characters that would normally be difficult to include in program code.
Newline: \n
skips to a
new line
Tab: \t
Indents text
Double-quotes: \"
Inserts "double quotes"
Single-quote: \'
Inserts 'single quotes'
Backslash: \\
Inserts a \ backslash
Here's some code that uses escape sequences:
puts "first line\nsecond line"
puts "\tindented text"
puts "He said, \"Whoa.\""
Output:
first line
second line
indented text
He said, "Whoa."
You don't always have to escape quotation marks in your strings:
puts "Double-quoted strings can hold single quotes: ''"
puts 'Single-quoted strings can hold double quotes: ""'
# But...
puts "Double-quotes in double-quoted strings need to be \"escaped\"."
puts 'Single-quotes in single-quoted strings need to be \'escaped\'.'
Additional practice
We've created a workshop where you can get additional practice with Ruby strings. Don't miss this chance to strengthen your skills!
Visit the workshop here: Practice Strings in Ruby
-
0:00
Sometimes, we need characters in a string that are hard to
-
0:02
represent in program code.
-
0:05
The first line of code prints a string that includes a newline character.
-
0:08
But it's awkward to read,
-
0:10
with the second half of the string starting on the second line.
-
0:14
We want line 3 to include a tab character to indent some text, but
-
0:18
when I press the tab key, my editor inserted space characters, instead.
-
0:22
And line 4 isn't even valid Ruby code.
-
0:25
It's supposed to be a single string that contains double quote marks.
-
0:28
But what we wind up with is a string containing he said, some invalid Ruby code
-
0:34
referring to a non-existent object named Whoa, and a second string, which is empty.
-
0:41
To address this problem, Ruby offers escape sequences.
-
0:46
An escape sequence is a portion of a string that represents a character that
-
0:50
would normally be hard to represent in program code.
-
0:53
Each of these two character combinations you see here
-
0:56
actually represents a single character.
-
0:59
So \ followed by n, represents a new line character, which skips to a new line.
-
1:05
\ followed by t, represents a tab character, which indents text.
-
1:10
\ followed by a " character inserts double quotes, even into a double quoted string.
-
1:16
\ followed by a ' character inserts single quotes,
-
1:20
again, even into a single quoted string.
-
1:22
And because you're going to need a way to insert backslash characters without Ruby
-
1:26
thinking you're starting an escape sequence,
-
1:28
\\ inserts a single backslash character.
-
1:33
Let's update the previous program to use escape sequences for
-
1:37
the hard to represent characters.
-
1:39
So first, let's take the new line character that's in the middle of this
-
1:42
first line and replace it with \n to represent an actual new line character.
-
1:48
Let's take the two spaces here on the second line and replace it
-
1:51
with \t which represents a tab character, that should indent the text for us.
-
1:57
And let's take the double quote characters that Ruby is mistaking for
-
2:00
the end of a string, and escape those using backslashes as well.
-
2:04
So that all of this becomes a single string.
-
2:07
Let's save that, and try rerunning it.
-
2:12
And everything works.
-
2:13
Our \n here on the first line,
-
2:16
gets replaced with a new line character down below.
-
2:18
\t gets replaced with a tab character.
-
2:22
And our \" get replaced with actual quotation marks.
-
2:28
Just so you know, you don't always have to escape quotation marks into your strings.
-
2:33
Double quoted strings can hold single quotes without escaping them.
-
2:36
Single quoted strings can hold double quotes without escaping them.
-
2:40
But, double quote characters within double quoted strings need to be escaped.
-
2:44
And so do single quotes within single quoted strings.
-
2:48
So, now that we know about escape sequences,
-
2:50
we know that the \n at the end of our answer variable is a new line character.
-
2:56
When the user presses the enter key to signal they're finished entering
-
2:59
an answer, it adds a new line character on the end of the string,
-
3:03
which gets returned by gets.
-
3:06
To remove it, we'll need to call a method on the string object,
-
3:09
which we'll learn how to do in the next stage.
-
3:12
Knowing how to work with strings is another essential programming skill,
-
3:15
one that's definitely worth practicing.
-
3:17
Be sure to check the teacher's notes to learn ways to get more practice with
-
3:21
strings.
-
3:22
Now you've learned the basics of working with strings in Ruby.
-
3:25
You can append strings to other strings by concatenating them.
-
3:29
You can embed data within strings using interpolation.
-
3:32
And you can embed special characters using escape sequences.
-
3:37
But that's only a small fraction of the features Ruby offers for
-
3:40
working with strings.
-
3:41
To do more, you're going to need to know how to call methods directly on
-
3:45
the strings, and not just for strings.
-
3:48
Virtually everything in Ruby has methods you can call on it.
-
3:51
We'll learn how to do that in the next stage.
You need to sign up for Treehouse in order to download course files.
Sign up