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.Sprintf
to 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
- Value:
[]int64{0, 1}
Format | Verb | Description |
---|---|---|
[0 1] | %v | Default format |
[]int64{0, 1} | %#v | Go-syntax format |
[]int64 | %T | The type of the value |
Integer (indent, base, sign)
- Value:
15
Format | Verb | Description |
---|---|---|
15 | %d | Base 10 |
+15 | %+d | Always show sign |
␣␣15 | %4d | Pad with spaces (width 4, right justified) |
15␣␣ | %-4d | Pad with spaces (width 4, left justified) |
0015 | %04d | Pad with zeroes (width 4) |
1111 | %b | Base 2 |
17 | %o | Base 8 |
f | %x | Base 16, lowercase |
F | %X | Base 16, uppercase |
0xf | %#x | Base 16, with leading 0x |
Character (quoted, Unicode)
- Value:
65
(Unicode letter A)
Format | Verb | Description |
---|---|---|
A | %c | Character |
'A' | %q | Quoted character |
U+0041 | %U | Unicode |
U+0041 'A' | %#U | Unicode 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
Format | Verb | Description |
---|---|---|
1.234560e+02 | %e | Scientific notation |
123.456000 | %f | Decimal point, no exponent |
123.46 | %.2f | Default width, precision 2 |
␣␣123.46 | %8.2f | Width 8, precision 2 |
123.456 | %g | Exponent as needed, necessary digits only |
String or byte slice (quote, indent, hex)
- Value:
'café'
Format | Verb | Description |
---|---|---|
café | %s | Plain string |
␣␣café | %6s | Width 6, right justify |
café␣␣ | %-6s | Width 6, left justify |
'café' | %q | Quoted string |
636166c3a9 | %x | Hex dump of byte values |
63 61 66 c3 a9 | % x | Hex dump with spaces |
Special values
Value | Description |
---|---|
a | U+0007 alert or bell |
b | U+0008 backspace |
| U+005c backslash |
t | U+0009 horizontal tab |
n | U+000A line feed or newline |
f | U+000C form feed |
r | U+000D carriage return |
v | U+000b vertical tab |
Arbitrary values can be encoded with backslash escapes andcan be used in any '
string literal.
There are four different formats:
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: