:@printf
@printf([io::IOStream], "%Fmt", args...)
Print args
using C printf()
style format specification string. Optionally, an IOStream
may be passed as the first argument to redirect output.
Examples
-
Print formatted output to the standard output:
julia> @printf("Hello, %s!", "Julia") Hello, Julia!
This example prints the formatted string "Hello, Julia!" to the standard output.
-
Print formatted output to a file:
julia> file = open("output.txt", "w"); julia> @printf(file, "This is a formatted output.") julia> close(file)
Here, the formatted string "This is a formatted output." is printed to a file named "output.txt".
-
Formatting numbers:
julia> @printf("%.2f", 3.14159) 3.14
In this example, the number 3.14159 is formatted to have 2 decimal places and printed.
- Combining multiple format specifiers:
julia> @printf("Name: %s, Age: %d, Height: %.1f", "Alice", 25, 1.65) Name: Alice, Age: 25, Height: 1.6
It showcases how to use multiple format specifiers to print different types of values in a single formatted string.
Common mistake example:
julia> @printf("The value is %d", "abc")
ERROR: MethodError: no method matching @printf(::LineNumberNode, ::Module, ::String, ::String)
In this example, the format specifier %d
expects an integer value, but a string value is provided instead. Ensure that the format specifiers match the type of the values being passed to @printf
to avoid such errors.
See Also
:@printf, :@sprintf, display, displayable, dump, info, isprint, print, println, print_escaped, print_joined, print_shortest, print_unescaped, print_with_color, pushdisplay, redisplay, show, showall, showcompact, sprint, versioninfo,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.