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()

numStr = "5"
print('Original String :', numStr)

# Left pad the string with 0 to make it's length 4
numStr = numStr.zfill(4)

print('Updated String :' , numStr)

Output:
Original String : 5
Updated String : 0005

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(s, width[, fillchar])

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,
numStr = "5"
print('Original String :', numStr)

# Make string right justified of length 4 by padding 3 spaces to left
numStr = numStr.rjust(4, ' ')

print('Updated String :', numStr)

Output:
Original String : 5
Updated String :    5

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.

numStr = "5"
print('Original String :', numStr)

# Make string right justified of length 4 by padding 3 '-' to left
numStr = numStr.rjust(4, '-')

print('Updated String :', numStr)

Output:
Original String : 5
Updated String : ---5

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(s, width[, fillchar])

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,
numStr = "45"
print('Original String :', numStr)

# Make string left justified of length 5 by padding 3 0s to the right of it
numStr = numStr.ljust(5, '0')

print('Updated String :', numStr)

Output:
Original String : 45
Updated String : 45000

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()

userName = "John"
print('Original String :', userName)

# Make string left justified of length 7 by padding 3 spaces to the right of it
userName = userName.ljust(7, ' ')

print('Updated String :' , userName, 'is')

Output:
Original String : John
Updated String : John    is

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.

userName = "John"
print('Original String :', userName)

# Make string left justified of length 7 by padding 3 '-' to the right of it
userName = userName.ljust(7, '-')

print('Updated String :' , userName)

Output:
Original String : John
Updated String : John---

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

Complete example is as follows,

def main():

    print('**** Left pad a string with 0 using s.zfill() ****')

    numStr = "5"
    print('Original String :', numStr)

    # Left pad the string with 0 to make it's length 4
    numStr = numStr.zfill(4)

    print('Updated String :' , numStr)


    print('**** Left pad a string with space using string.rjust() ****')

    numStr = "5"
    print('Original String :', numStr)

    # Make string right justified of length 4 by padding 3 spaces to left
    numStr = numStr.rjust(4, ' ')

    print('Updated String :', numStr)

    print('**** Left pad a string with a character using string.rjust() ****')

    numStr = "5"
    print('Original String :', numStr)

    # Make string right justified of length 4 by padding 3 '-' to left
    numStr = numStr.rjust(4, '-')

    print('Updated String :', numStr)

    print('**** Right pad a string with zeros using string.rjust() ****')

    numStr = "45"
    print('Original String :', numStr)

    # Make string left justified of length 5 by padding 3 0s to the right of it
    numStr = numStr.ljust(5, '0')

    print('Updated String :', numStr)


    print('**** Right pad a string with space using string.rjust() ****')

    userName = "John"
    print('Original String :', userName)

    # Make string left justified of length 7 by padding 3 spaces to the right of it
    userName = userName.ljust(7, ' ')

    print('Updated String :' , userName, 'is')

    print('**** Right pad a string with a character using string.rjust() ****')

    userName = "John"
    print('Original String :', userName)

    # Make string left justified of length 7 by padding 3 '-' to the right of it
    userName = userName.ljust(7, '-')

    print('Updated String :' , userName)


if __name__ == '__main__':
   main()


Output:
**** Left pad a string with 0 using s.zfill() ****
Original String : 5
Updated String : 0005
**** Left pad a string with space using string.rjust() ****
Original String : 5
Updated String :    5
**** Left pad a string with a character using string.rjust() ****
Original String : 5
Updated String : ---5
**** Right pad a string with zeros using string.rjust() ****
Original String : 45
Updated String : 45000
**** Right pad a string with space using string.rjust() ****
Original String : John
Updated String : John    is
**** Right pad a string with a character using string.rjust() ****
Original String : John
Updated String : John---

 

 

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.