How to use the Python center() method?

Python Center() Method

Hello, folks! In this article, we will be understanding Python center() function with String, NumPy, and Pandas module.


Python center() method with string

Python String has a lot of in-built functions to manipulate and deal with strings.

The string.center() method performs padding on the string with a specific character on both the sides(left and right side) of the input string in a centralized form.

Syntax:

string.center(width,fillchar)
  • width: This value decides the padding area around the string.
  • fillchar: The padding area is filled by a particular character. The default character is space.

Example 1:

inp_str = "Python with JournalDev"
print("Input string: ", inp_str)
res_str = inp_str.center(36) 
print("String after applying center() function: ", res_str)

In the above snippet of code, using the center() function we have padded the string with the default fillchar i.e. spaces by the width(36) defined in the parameter list.

Output:

Input string:  Python with JournalDev
String after applying center() function:         Python with JournalDev    

Example 2: Padding the string with center() function by a particular fillchar

inp_str = "Python with JournalDev"
print("Input string: ", inp_str)
res_str = inp_str.center(36,"$") 
print("String after applying center() function: ", res_str)

Output:

Input string:  Python with JournalDev
String after applying center() function:  $$$$$$$Python with JournalDev$$$$$$$

Python center() function with Pandas module

Python center() function can also be used along with the DataFrames of Pandas module.

The DataFrame.str.center() function fill the input string by the character passed to the function along a particular width(including the width of the string) on both the sides of the string.

Syntax:

DataFrame.str.center(width,fillchar)

Input Dataset:

        contact
1	cellular
2	telephone
3	cellular
4	cellular
5	telephone
6	telephone
7	telephone
8	cellular
9	cellular

Example:

import pandas
info=pandas.read_csv("C:/marketing_tr.csv")
info_con=pandas.DataFrame(info['contact'].iloc[1:10])
info_con['contact']=info_con['contact'].str.center(width = 15, fillchar = '%') 
print(info_con['contact'])

Output:

1    %%%%cellular%%%
2    %%%telephone%%%
3    %%%%cellular%%%
4    %%%%cellular%%%
5    %%%telephone%%%
6    %%%telephone%%%
7    %%%telephone%%%
8    %%%%cellular%%%
9    %%%%cellular%%%
Name: contact, dtype: object

Python center() function with NumPy module

Python center() function can be used along with NumPy module to perform center padding on every element of the array.

The numpy.char.center() method is used to center the elements and even performs padding on it by a particular character on both the sides of the array elements.

Syntax:

numpy.char.center(array,width,fillchar)

Example:

import numpy as np 

inp_arr = ['Python', 'Java', 'Kotlin', 'C'] 
print ("Input Array : ", inp_arr) 
res_arr = np.char.center(inp_arr, 15, fillchar = '%')
print("Array after applying center() function: ", res_arr)

In the above example, we have applied the center() function to every element of the array and thus have centered the elements and padded the array elements with the fillchar according to the width on both the sides.

Output:

Input Array :  ['Python', 'Java', 'Kotlin', 'C']
Array after applying center() function:  ['%%%%%Python%%%%' '%%%%%%Java%%%%%' '%%%%%Kotlin%%%%' '%%%%%%%C%%%%%%%']

Conclusion

Thus, in this article, we have understood the working of Python center() function along with NumPy and Pandas module, respectively.


References

  • Python center() function — JournalDev