This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses 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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up