← Back to Ruby Course | Chapter 1: Setup & Basics | Lesson 5 of 7

puts, print, and p

puts, print, and p are three ways to show things on screen: puts adds a new line, print stays on the same line, and p shows the value the way a programmer sees it. They are like three different ways of speaking out loud.

In this page:

  1. puts, print, and p
Syntax
ruby
puts value
print value
p value

puts, print, and p

puts prints a value followed by a newline, print prints without adding one, and p prints the value's inspected (debug) representation, including quotes around strings.

p is especially useful while debugging since it makes the type of the value clearer than puts. All three accept multiple arguments.

Note: Use p instead of puts when debugging, since it shows exactly what type of value you have, quotes and all.

Example: puts, print, and p

markup
puts "Hello with puts"
print "Hello with print"
print "\n"
p "Hello with p"
p [1, 2, 3]
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using puts on an array and being surprised that each item is on its own line, when p shows the array with brackets.
  2. Expecting print "Hello" to end the line, when it adds no newline.
  3. Using p for output shown to users, when it prints the inspect format with quotes.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.