FAQ 4.33 How do I pad a string with blanks or pad a number with zeroes?

Discussion in 'Perl Misc' started by PerlFAQ Server, Mar 13, 2011.

  1. This is an excerpt from the latest version perlfaq4.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .


    4.33: How do I pad a string with blanks or pad a number with zeroes?

    In the following examples, $pad_len is the length to which you wish to
    pad the string, $text or $num contains the string to be padded, and
    $pad_char contains the padding character. You can use a single character
    string constant instead of the $pad_char variable if you know what it is
    in advance. And in the same way you can use an integer in place of
    $pad_len if you know the pad length in advance.

    The simplest method uses the "sprintf" function. It can pad on the left
    or right with blanks and on the left with zeroes and it will not
    truncate the result. The "pack" function can only pad strings on the
    right with blanks and it will truncate the result to a maximum length of

    # Left padding a string with blanks (no truncation):
    $padded = sprintf("%${pad_len}s", $text);
    $padded = sprintf("%*s", $pad_len, $text); # same thing

    # Right padding a string with blanks (no truncation):
    $padded = sprintf("%-${pad_len}s", $text);
    $padded = sprintf("%-*s", $pad_len, $text); # same thing

    # Left padding a number with 0 (no truncation):
    $padded = sprintf("%0${pad_len}d", $num);
    $padded = sprintf("%0*d", $pad_len, $num); # same thing

    # Right padding a string with blanks using pack (will truncate):
    $padded = pack("A$pad_len",$text);

    If you need to pad with a character other than blank or zero you can use
    one of the following methods. They all generate a pad string with the
    "x" operator and combine that with $text. These methods do not truncate

    Left and right padding with any character, creating a new string:

    $padded = $pad_char x ( $pad_len - length( $text ) ) . $text;
    $padded = $text . $pad_char x ( $pad_len - length( $text ) );

    Left and right padding with any character, modifying $text directly:

    substr( $text, 0, 0 ) = $pad_char x ( $pad_len - length( $text ) );
    $text .= $pad_char x ( $pad_len - length( $text ) );


    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    PerlFAQ Server, Mar 13, 2011
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cjl
    Bengt Richter
    Mar 26, 2005
  2. Hans Terlouw

    Pyrex: problem with blanks in string

    Hans Terlouw, Jun 4, 2007, in forum: Python
    Michael Hoffman
    Jun 4, 2007
  3. PerlFAQ Server
    PerlFAQ Server
    Feb 2, 2011
  4. PerlFAQ Server
    PerlFAQ Server
    Feb 5, 2011
  5. PerlFAQ Server
    PerlFAQ Server
    Mar 27, 2011