Menu
Coddy logo textTech

Output with print and p

Part of the Fundamentals section of Coddy's Ruby journey — lesson 27 of 88.

Ruby gives you two more ways to display output: print and p. Each behaves differently from puts.

The print method outputs text without adding a new line at the end:

print "Hello"
print "World"

This outputs:

HelloWorld

Notice how both words appear on the same line. This is useful when you want to build output piece by piece without automatic line breaks.

The p method is different. It shows the raw, debug-friendly version of a value:

puts "Hello"
p "Hello"

This outputs:

Hello
"Hello"

With p, strings appear with their quotation marks, making it clear you're looking at a string. This is helpful when debugging because you can see exactly what type of data you're working with.

challenge icon

Challenge

Easy

Use print and p to display output in the following ways:

  1. Use print to output Ruby followed by another print to output Programming (both should appear on the same line with no space between them), then use a bare puts (with no arguments) to move to the next line
  2. Use p to output the string debug (showing its raw form with quotation marks)
  3. Use p to output the number 42

Your output should be exactly 3 lines total:

RubyProgramming<br>"debug"<br>42

Cheat sheet

Ruby provides print and p as alternatives to puts for displaying output.

The print method outputs text without adding a new line:

print "Hello"
print "World"
# Outputs: HelloWorld

The p method displays the raw, debug-friendly version of a value, showing strings with quotation marks:

puts "Hello"  # Outputs: Hello
p "Hello"     # Outputs: "Hello"

This makes p useful for debugging, as it clearly shows the data type you're working with.

Try it yourself

# TODO: Write your code below

# 1. Use print twice to output "Ruby" and "Programming" on the same line,
#    then use a bare puts (with no arguments) to move to the next line

# 2. Use p to output the string "debug"

# 3. Use p to output the number 42
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals