Python | Pandas Series.str.center()

Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.center() function is used for filling left and right side of strings in the Series/Index with an additional character. The function is equivalent to Python’s str.center().

Syntax: Series.str.center(width, fillchar=’ ‘)

Parameter :
width : Minimum width of resulting string; additional characters will be filled with fillchar
fillchar : Additional character for filling, default is whitespace



Returns : filled

Example #1: Use Series.str.center() function to fill the left and right side of the string in the underlying data of the given series object by ‘*’ symbol.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New_York', 'Lisbon', 'Tokyo', 'Paris', 'Munich'])
  
# Creating the index
idx = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)

chevron_right


Output :

Now we will use Series.str.center() function to fill ‘*’ symbol in the left and right side of the string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# fill '*' in the left and right side of string
result = sr.str.center(width = 13, fillchar = '*')
  
# print the result
print(result)

chevron_right


Output :

As we can see in the output, the Series.str.center() function has successfully filled ‘*’ symbol in the left and the right side of the string in the underlying data of the given series object.

Example #2 : Use Series.str.center() function to fill the left and right side of the string in the underlying data of the given series object by ‘*’ symbol.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['Mike', 'Alessa', 'Nick', 'Kim', 'Britney'])
  
# Creating the index
idx = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)

chevron_right


Output :


Now we will use Series.str.center() function to fill ‘*’ symbol in the left and right side of the string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# fill '*' in the left and right side of string
# width after filling should be 5
result = sr.str.center(width = 5, fillchar = '*')
  
# print the result
print(result)

chevron_right


Output :

As we can see in the output, the Series.str.center() function has successfully filled ‘*’ symbol in the left and the right side of the string in the underlying data of the given series object.

Note : If the value of width is smaller than the length of actual string then the whole string is printed without truncating it.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.