Open In App

Add padding to a string in Python

Last Updated : 14 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Inserting non-informative characters into a string is known as Padding. The insertion can be done anywhere on the string. String padding is useful for output formatting and alignment of strings while printing. Even for displaying values like a table string Padding is required. In this article, we will see how to Pad Zeros to a String in Python.

Different Types of Padding

The three most commonly used String Padding techniques are:

  • Inserting a character to the end of the String to make it of a specified length is known as Left Padding. It is most helpful when naming files that mostly start with numeric characters which are generated in sequence.
  • In the case of Center Padding, the required character is inserted at both the ends of the strings an equal number of times until the newly formed string is of the specific length. This centers the string of the specific or required length. This returns a string centered on length and width. Here, the default character is space.
  • In Right Padding, the given character is inserted to the right side of the String which needs to be padded. The specified character is added to the end of the string until it attains the required length.

Pad a String with Zeros or Spaces in Python can be achieved using various in-built functions, each of them is discussed below with an example:

Method 1: Pad Zeros to a String using ljust method.

Using this method, the string is aligned to the left side by inserting padding to the right end of the string.

Syntax : string.ljust(width, char)

Parameters:

  • Width -It is the length of the string after the padding is complete. Compulsory.
  • Char– string to be padded, optional.

Returns: Returns the string which is remaining after the length of the specified width.

Example: Left padding a String with Zeros using ljust() method

Python3




# Creating a string variable
str = "Geeks for Geeks"
 
# Printing the output of ljust() method
print(str.ljust(20, '0'))


Output:

Geeks for Geeks00000

Method 2: Pad Zeros to a String using rjust() method

Using this method, the String is aligned to right by insertion of characters to the left end of the String. 

Syntax: string.rjust(width, char)

Parameters:

  • Width -It is the length of the string after the padding is complete. Compulsory.
  • Char– string to be padded, optional.

Returns: returns a String which is placed right side with characters inserted to the beginning end.

Example: Right padding a String with Zeros using rjust() method

Python3




# Creating a string variable
str = "Geeks for Geeks"
 
# Using rjust() method
print(str.rjust(20, "O"))


Output:

OOOOOGeeks for Geeks

Method 3: Pad Zeros to a String using the center() function

The center() method takes the string and aligns it in the center with the given width as a parameter. 

Syntax: string.center(width)

Parameter:

This method just takes one parameter which is the width and it adds white spaces to both the ends.

Returns:

Returns the string padded to the center.

Example: Center padding a String with space/zeros using center() function.

Python3




# Creating a string variable
str = "Geeks for Geeks"
 
# Using center() method
print("Center String: ", str.center(20))


Output:

Center String:    Geeks for Geeks

Method 4: Pad Zeros to a String using zfill() method

The zfill() method inserts 0s to the beginning of the String. This method does padding to the right by inserting characters to the left of the String with 0s until the string is of a specific length. In case when the parameter passed is less than the length or size of the string, then no padding takes place. 

Syntax: string.zfill(length)

Parameter: takes a length as its only parameter.

Example: Left padding a String with Zeros using zfill() method

Python3




# Creating string variables
v1 = "Geeks"
v2 = "for"
v3 = "Geeks"
 
# Using zfill() method
print(v1.zfill(10))
print(v2.zfill(10))
print(v3.zfill(10))


Output:

00000Geeks
0000000for
00000Geeks

Method 5: Pad Zeros to a String using format() function

The format() method can be used for all types of paddings namely left, right and center. 

Syntax: string.format(value_1, value_2, value_3, value_4… value_n)

Parameters: specific values to be formatted. The placeholders can be indexes or just empty white spaces too.

Returns: a formatted string

Example:

Python3




# Creating string variable
str = "Cost of mask: {price:.2f} Rupees."
 
# Using format() method
print(str.format(price=20))


Output:

Cost of mask: 20.00 Rupees.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads