Printf Cheat Sheet



Apr 11, 2015 Printf April 11, 2015 April 12, 2015 Trey Shaffer IO Streams, Output Streams Java’s printf is a method that is used for formatting data output and is a combination of String.format and out.print. Perl sprintf Summary Usage:. printf FILEHANDLE FORMAT, LIST. printf FORMAT, LIST. sprintf FORMAT, LIST Format summary: Format Description%% a percent sign%c a character with the given number%s a string%d a signed integer, in decimal%u an unsigned integer, in decimal%o an unsigned integer, in octal%x anuns ig e dt r, h x ec m l. Java printf( ) Method Quick Reference. System.out.printf( “format-string” , arg1, arg2); Format String: Composed of literals and format specifiers. Arguments are required only if there are format specifiers in the format string. Format specifiers include: flags, width, precision, and conversion characters in the following sequence.

  • Basics
  • Cheat sheet

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. As I usually print cheat sheets two pages per side and the pack/unpack cheat sheet consumed just one page, I added Perl's printf/sprintf format and attribute summary. Here is how I printed this cheat sheet: Download Perl pack/unpack and printf Cheat Sheet.

Basics

With the Go fmt packageyou can format numbers and strings padded with spaces or zeroes,in different bases, and with optional quotes.

You submit a template string that contains the text you want to formatplus some annotation verbs that tell the fmt functionshow to format the trailing arguments.

Printf

In this example, fmt.Printf formats and writes to standard output:

  • the template string is 'Binary: %b%b',
  • the annotation verb%b formats a number in binary, and
  • the special value is a backslash.

As a special case, the verb %%, which consumes no argument, produces a percent sign:

Sprintf (format without printing)

Use fmt.Sprintfto format a string without printing it:

Find fmt errors with vet

If you try to compile and run this incorrect line of code

you’ll find that the program will compile, and then print Steam play together games.

To catch this type of errors early, you can usethe vet command – it can findcalls whose arguments do not align with the format string.

Cheat sheet

Default formats and type

Printf
  • Value:[]int64{0, 1}
FormatVerbDescription
[0 1]%vDefault format
[]int64{0, 1}%#vGo-syntax format
[]int64%TThe type of the value

Integer (indent, base, sign)

  • Value:15
FormatVerbDescription
15%dBase 10
+15%+dAlways show sign
␣␣15%4dPad with spaces (width 4, right justified)
15␣␣%-4dPad with spaces (width 4, left justified)
0015%04dPad with zeroes (width 4)
1111%bBase 2
17%oBase 8
f%xBase 16, lowercase
F%XBase 16, uppercase
0xf%#xBase 16, with leading 0x
Printf Cheat Sheet

Character (quoted, Unicode)

  • Value:65 (Unicode letter A)
Printf Cheat Sheet
FormatVerbDescription
A%cCharacter
'A'%qQuoted character
U+0041%UUnicode
U+0041 'A'%#UUnicode with character

Boolean (true/false)

Use %t to format a boolean as true or false.

Pointer (hex)

Use %p to format a pointer in base 16 notation with leading 0x.

Float (indent, precision, scientific notation)

  • Value:123.456
FormatVerbDescription
1.234560e+02%eScientific notation
123.456000%fDecimal point, no exponent
123.46%.2fDefault width, precision 2
␣␣123.46%8.2fWidth 8, precision 2
123.456%gExponent as needed, necessary digits only

String or byte slice (quote, indent, hex)

  • Value:'café'
Cheat
FormatVerbDescription
café%sPlain string
␣␣café%6sWidth 6, right justify
café␣␣%-6sWidth 6, left justify
'café'%qQuoted string
636166c3a9%xHex dump of byte values
63 61 66 c3 a9% xHex dump with spaces

Special values

ValueDescription
aU+0007 alert or bell
bU+0008 backspace
U+005c backslash
tU+0009 horizontal tab
nU+000A line feed or newline
fU+000C form feed
rU+000D carriage return
vU+000b vertical tab

Arbitrary values can be encoded with backslash escapes andcan be used in any ' string literal.

There are four different formats:

Printf Cheat Sheet
  • x followed by exactly two hexadecimal digits,
  • followed by exactly three octal digits,
  • u followed by exactly four hexadecimal digits,
  • U followed by exactly eight hexadecimal digits.

Free music making software. The escapes u and U represent Unicode code points.

Further reading

C Printf Cheat Sheet

Share this page: