Escape Sequences3:54 with Jay McGavren
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.
skips to a new line
Inserts "double quotes"
Inserts 'single quotes'
Inserts a \ backslash
Here's some code that uses escape sequences:
puts "first line\nsecond line" puts "\tindented text" puts "He said, \"Whoa.\""
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\'.'
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
You need to sign up for Treehouse in order to download course files.Sign up