Add padding to a string in Python - GeeksforGeeks

15 Dec, 2021
Inserting non-informative characters to 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. 
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 mostly 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 with 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 of length 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.
Padding can be achieved using various in-built functions, each of them is discussed below with an example:
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 
Python3
# Creating a string variablestr = "Geeks for Geeks" # Printing the output of ljust() methodprint(str.ljust(20, '0'))
Output:
Geeks for Geeks00000
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:
Python3
# Creating a string variablestr = "Geeks for Geeks" # Using rjust() methodprint(str.rjust(20, "O"))
Output:
OOOOOGeeks for Geeks
The center() method takes the string and aligns it in the center with the given width as 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:
Python3
# Creating a string variablestr = "Geeks for Geeks" # Using center() methodprint("Center String: ", str.center(20))
Output:
Center String:    Geeks for Geeks
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 of 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:
Python3
# Creating string variablesv1 = "Geeks"v2 = "for"v3 = "Geeks" # Using zfill() methodprint(v1.zfill(10))print(v2.zfill(10))print(v3.zfill(10))
Output:
00000Geeks
0000000for
00000Geeks
The format() method can be used for all the type 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 variablestr = "Cost of mask: {price:.2f} Rupees." # Using format() methodprint(str.format(price=20))
Output:
Cost of mask: 20.00 Rupees.
anikakapoor
Picked
python-string
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
*args and **kwargs in Python
Check if element exists in list in Python
Convert integer to string in Python