Skip to content
Related Articles

Related Articles

Improve Article

Add padding to a string in Python

  • Difficulty Level : Easy
  • Last Updated : 17 Dec, 2020

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. 

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 mostly helpful when naming files which 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:

ljust()

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

Syntax : tring.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 variable
str = "Geeks for Geeks"
  
# Printing the output of ljust() method
print(str.ljust(20, '0'))

Output:

Geeks for Geeks00000

rjust()

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 variable
str = "Geeks for Geeks"
  
# Using rjust() method
print(str.rjust(20, "O"))

Output:

OOOOOGeeks for Geeks

center()

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 variable
str = "Geeks for Geeks"
  
# Using center() method
print("Center String: ", str.center(20))

Output:



Center String:    Geeks for Geeks

zfill()

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

format()

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 variable
str = "Cost of mask: {price:.2f} Rupees."
  
# Using format() method
print(str.format(price=20))

Output:

Cost of mask: 20.00 Rupees.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :