Formatted printing in Perl using printf and sprintf

printf sprintf

printf can be used to create a formatted print to the screen.

sprintf accepts the same parameters, but it will return the formatted string instead of printing it so you can collect the string and print them later, or save them to a file or send them in an e-mail.

examples/printf.pl


my $v = 65;
printf("<%s>\n", $v);     # <65>
printf("<%10s>\n", $v);   # <      65>
printf("<%-10s>\n", $v);  # <65      >
printf("<%c>\n", $v);     # <A>
printf("<%d>\n", $v);     # <65>
printf("<%0.5d>\n", $v);  # <00065>

Other pages

Perl tutorial

Author

Gabor Szabo (szabgab) Gabor Szabo