Perl printf format examples (reference page)

Here’s a reference page (cheat sheet) of Perl printf formatting options. Hopefully this list covers the most common Perl printf printing options you’ll run into, or will at least point you in the right direction.

Perl ‘printf’ string formatting

Here are several examples that show how to format strings with Perl and printf. I’ll use single quotes in all my printf examples to help demonstrate left- and right-justification.

# a simple string
printf("'%s'\n", "Hello");

# a string with a minimum length, right-justified
printf("'%20s'\n", "Hello");

# minimum length, left-justified
printf("'%-20s'\n", "Hello");

That code results in the following output:

'Hello'
'               Hello'
'Hello               '

Perl printf: formatting integers

The following code demonstrates how to print integers with Perl, using the printf function. These examples show how to control field widths, printing left-justified, right-justified, and zero-filled.

# print five characters wide
printf("right-justified (default) integer output:\n");
printf("'%5d'\n", 0);
printf("'%5d'\n", 123456789);
printf("'%5d'\n", -10);
printf("'%5d'\n", -123456789);
printf("\n");

# five characters wide, left justified
printf("left-justified integer output:\n");
printf("'%-5d'\n", 0);
printf("'%-5d'\n", 123456789);
printf("'%-5d'\n", -10);
printf("'%-5d'\n", -123456789);
printf("\n");

# five characters wide, zero-filled integer output
printf("zero-filled integer output:\n");
printf("'%05d'\n", 0);
printf("'%05d'\n", 1);
printf("'%05d'\n", 123456789);
printf("'%05d'\n", -10);
printf("'%05d'\n", -123456789);

And here’s the output from that source code:

right-justified (default) integer output:
'    0'
'123456789'
'  -10'
'-123456789'

left-justified integer output:
'0    '
'123456789'
'-10  '
'-123456789'

zero-filled integer output:
'00000'
'00001'
'123456789'
'-0010'
'-123456789'

Formatting floating-point numbers

The following Perl printf code demonstrates how to format floating-point output:

printf("one position after the decimal\n");
printf("'%.1f'\n\n", 10.3456);

printf("two positions after the decimal\n");
printf("'%.2f'\n\n", 10.3456);

printf("eight wide, two positions after the decimal\n");
printf("'%8.2f'\n\n", 10.3456);

printf("eight wide, four positions after the decimal\n");
printf("'%8.4f'\n\n", 10.3456);

printf("eight-wide, two positions after the decimal, zero-filled\n");
printf("'%08.2f'\n\n", 10.3456);

printf("eight-wide, two positions after the decimal, left-justified\n");
printf("'%-8.2f'\n\n", 10.3456);

printf("printing a much larger number with that same format\n");
printf("'%-8.2f'\n", 101234567.3456);

And here’s the output from those printf floating-point (decimal) examples:

one position after the decimal
'10.3'

two positions after the decimal
'10.35'

eight wide, two positions after the decimal
'   10.35'

eight wide, four positions after the decimal
' 10.3456'

eight-wide, two positions after the decimal, zero-filled
'00010.35'

eight-wide, two positions after the decimal, left-justified
'10.35   '

printing a much larger number with that same format
'101234567.35'

Formatting currency

Hopefully you can see from that example that one way to print currency is with two positions after the decimal, like this:

printf("two positions after the decimal\n");
printf("'%.2f'\n\n", 10.3456);

This works for many simple programs, but for more robust programs you’ll probably want to do more work than this.

Printing tabs, slashes, backslashes, newlines

Here are a few Perl printf examples that demonstrate how to print other characters in your output, including tabs, slashes, backslashes, and newlines.

printf("how you normally print a newline character\n");
printf("at the end of this string there is a newline character\n\n");

printf("print a TAB character in a string\n");
printf("hello\tworld\n\n");

printf("print a newline character in a string\n");
printf("hello\nworld\n\n");

printf("print a single backslash by putting two in your string\n");
printf("C:\\Windows\\System32\\\n\n");

printf("forward slashes are easier to print\n");
printf("/Users/al/tmp\n");

A combination of printf techniques

Here’s a little example that demonstrates several of these techniques in one printf statement:

printf("the %s jumped over the %s ... %d times\n", "cow", "moon", 2);

This results in the following output:

the cow jumped over the moon ... 2 times

Related printf content

Here are a few links to other printf tutorials on our website: