In this article we will discuss how to do left padding of strings or right padding of strings either with zero, space or some other character.

Left padding of string in Python

Left padding a string means adding a given character at the left side of string to make it of a given length. Let’s understand by an examples,

Suppose we have a number string i.e. “5”. Now we want to convert this string of length 1 to a string of length 4 by,

  • Left padding three zeros to the string i.e. “0005”
  • Left padding three space to the string i.e. ”   5″
  • Left padding three characters to the string i.e. “—5”

Let’s see how to do that with examples,

Left pad a string with zeros using string.zfill()

Output:

string.zfill(s, width) pads a given string on the left with zeros (0) until length of string reaches the given width.

Here, 3 zeros are padded to the left of given string to make it’s length 4.

Left pad a string with space using string.rjust()

string.rjust()

string.rjust() makes the given string right justified by padding fillchar to the left of string to make it’s length equal to given width. Let’s use it,

Output:

Here, 3 spaces are padded to the left of given string to make it’s length 5.

Left pad a string with some character using string.rjust()

We can pass the fill character in string.rjust(s, width[, fillchar]) to left pad the given string by that give character (fillchar) i.e.

Output:

Here, three ‘-‘ are padded to the left of given string to make it’s length 4.

Right padding of string in Python

Right padding a string means adding a given character at the right side of string to make it of a given length. Let’s understand by an examples,

Suppose we have a number string i.e. “John”. Now we want to convert this string of length 4 to a string of length 7 by,

  • Right padding three zeros to the string i.e. “John000”
  • Right padding three space to the string i.e. “John   “
  • Right padding three characters to the string i.e. “John—“

Let’s see how to do that with examples,

Right pad a string with zeros using string.ljust()

string.ljust() makes the given string left justified by padding a given character (i.e. fillchar) to the left of string to make it’s length equal to given width. Let’s use it,

Output:

Here, 3 zeros are padded to the right of given string to make it’s length 5.

Right pad a string with space using string.ljust()

Output:

Here, 3 spaces are padded to the right of given string to make it’s length 7.

Right pad a string with some character using string.ljust()

We can pass the a fill character in string.ljust(s, width[, fillchar]) to right pad the given string by that character ( i.e. fillchar) i.e.

Output:

Here, three ‘-‘ are padded to the right of given string to make it’s length 7.

Complete example is as follows,

Output:

 

 

Python Resources

C++11 / C++14 Resources

Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,