Printf Cheat Sheet



If you want to print formatted text output using the System.out.println or System.err.println methods with the printf formatting options you’re used to with other languages, the easiest way to do that is to instead use the newer System.out.format or System.err.format methods.

Java printf formatting with System.out.format

For instance, here’s the usual “Hello, world” example, using these new Java formatting and printing methods:

Cheat

Comma operator is a binary operator, it evaluates its first operand and discards the result, and then evaluates the second operand and return this value. The C Cheat Sheet Revision 1.0 September 5, 2000. Printf function in the source code since this function is not a part of the core language. Printf formatting with Perl and Java In this cheat sheet I’ll show all the examples using Perl, but at first it might help to see one example using both Perl and Java. Therefore, here’s a simple Perl printf example to get things started: printf ('the%s jumped over the%s,%d times', 'cow', 'moon', 2).

While that example hardly makes it look very valuable, here’s a better Java printf-style formatting example that shows the power of these formatting methods:

To get the same output without using the new Java format method, you’d have to concatenate (merge) your Strings and variables together, which would be some ugly code, something like this:

(If you’re familiar with the printf or sprintf syntax from the C programming language, or similar methods in the Perl or Ruby languages, you’ll instantly be familiar with the syntax used with String.format.)

I find the approach using the format method much easier to write, read, and maintain.

So, as a quick summary, whenever you’re tempted to use those old System.out.println or System.err.println methods, and you need some nice, formatted output, consider using the newer System.out.format or System.err.format methods instead.

printf formatting reference

If you’d like to see much more information about formatting options you can use with the Java System.out.format() method, check this :

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.

printf formatting with Perl and Java

In this cheat sheet I’ll show all the examples using Perl, but at first it might help to see one example using both Perl and Java. Therefore, here’s a simple Perl printf example to get things started:

And here are three different Java printf examples, using different string formatting methods that are available to you in the Java programming language:

As you can see in that last String.format example, that line of code doesn’t print any output, while the first line prints to standard output, and the second line prints to standard error.

A summary of printf format specifiers

Here’s a quick summary of the available printf format specifiers:

Sheets
%ccharacter
%ddecimal (integer) number (base 10)
%eexponential floating-point number
%ffloating-point number
%iinteger (base 10)
%ooctal number (base 8)
%sa string of characters
%uunsigned decimal (integer) number
%xnumber in hexadecimal (base 16)
%%print a percent sign
%print a percent sign

printf string formatting

Here are several examples that show how to format string output with printf:

DescriptionCodeResult
A simple stringprintf(“‘%s'”, “Hello”);‘Hello’
A string with a minimum lengthprintf(“‘%10s'”, “Hello”);‘ Hello’
Minimum length, left-justifiedprintf(“‘%-10s'”, “Hello”);‘Hello ‘

printf special characters

The following character sequences have a special meaning when used as printf format specifiers:

aaudible alert
bbackspace
fform feed
nnewline, or linefeed
rcarriage return
ttab
vvertical tab
backslash

As you can see from that last example, because the backslash character itself is treated specially, you have to print two backslash characters in a row to get one backslash character to appear in your output.

Here are a few examples of how to use these special characters:

DescriptionCodeResult
Insert a tab character in a stringprintf(“Hellotworld”);Hello world
Insert a newline character in a stringprintf(“Hellonworld”);Hello
world
Typical use of the newline characterprintf(“Hello worldn”);Hello world
A DOS/Windows path with backslash charactersprintf(“C:WindowsSystem32”);C:WindowsSystem32
Cheat

Do you remember learning about root words in your language classes? For English speakers, like myself, much of our language is comprised of Greek and Latin roots, with added prefixes and suffixes to create new words. Similarly, commands used across multiple languages routinely have a common naming convention and may even perform like tasks.

More Linux resources

The printf command traces its roots back to the development world but offers practical utility to the sysadmin, as well. Derived from the print function in C programming languages, it provides the user with the ability to print a formatted string of output. It works on text, numerical output, or a combination of the two. This can be powerful when paired with a variable.

The syntax

The command syntax of printf is pretty straightforward according to the man page, however, it can become confusing quickly. The basic syntax is as follows:

In the first example, FORMAT alters the output in the same manner that it would in the C languages.

Where it gets weird...

There are three types of objects described here: Standard characters, interpreted characters, and conversion specifications. Let's look at all three below:

1. Standard characters inserted directly into the output.

2. Interpreted characters marked by (seen below):

Common interpreted characters:

'double quote
backslash
bbackspace
cproduce no further output
eescape
nnew line
thorizontal tab
vvertical tab

3. Conversion specifications (changes how an argument is translated into the output).

Each conversion specification is noted by the % and ends with a conversion character (seen below):

d, iAn integer (expressed as a decimal)
uAn integer (expressed as an unsigned decimal)
x, XAn integer (expressed as an unsigned hexadecimal)
oAn integer (expressed as an unsigned octal)
sA string
cAn integer (expressed as ASCII)

There are others, however, these are some of the most common.

Next, let's look at usage.

[ You might also like: 11 Linux commands I can’t live without ]

Basic usage

Golang printf cheat sheet

The most basic use cases for printf is outputting strings of text without receiving an error.

Seen here:

If you wanted to get a little more advanced, you could do something like this:

Printf Cheat Sheet

The above example is only slightly more advanced due to the usage of a STRING. The FORMAT portion of the command is contained within the double-quotes. We have a conversion spec (STRING) that outputs 'Enable Sysadmin' in place of %s.

You can also use printf in conjunction with environmental variables to do some pretty neat things. A simple example of this can be tested by passing the following:

C programming cheat sheet

This passes the STRING as the environmental variable $LOGNAME, which is the username of the account executing the command. You can see the difference below after changing accounts:

Sorry, I couldn't help myself.

[ Want to test your sysadmin skills? Take a skills assessment today. ]

Printf Example

Wrap-up

Cheat Sheet Recipes

All jokes aside, the printf command can be a handy tool, specifically if you have some experience with the C programming languages already. If not, the utility is still there, but you will have to work a bit harder to draw on the command's full power.

Yahoo Fantasy Football Cheat Sheets

Check out these related articles on Enable Sysadmin