{"id":24460,"date":"2021-10-17T18:40:45","date_gmt":"2021-10-17T13:10:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=24460"},"modified":"2021-11-05T19:23:59","modified_gmt":"2021-11-05T13:53:59","slug":"program-for-python-string-center-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-for-python-string-center-method\/","title":{"rendered":"Program for Python String center() Method"},"content":{"rendered":"

In the previous article, we have discussed Python Program for casefold() Function<\/a>
\ncenter() Function in Python:<\/strong><\/p>\n

The center () method will center align the string, using a specified character as the fill character (space is the default).<\/p>\n

Syntax:<\/strong><\/p>\n

String.center(Length, character)<\/pre>\n

Parameters:<\/strong><\/p>\n

length: <\/strong>This is required. It is the length of the string returned.<\/p>\n

character: <\/strong>This is optional. Fills the blanks on each side with a character. The default value is ” ” (space).<\/p>\n

Note:<\/strong> This parameter is optional, and if omitted, the white spaces are used as the default parameter by the centre Function. Please specify the Characters you want to use in the remaining width if you want to change the default value.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given first string = \"python\"\r\nGiven length = 30 \r\nGiven character = '-'<\/pre>\n

Output:<\/strong><\/p>\n

The above given first string is : python\r\nThe given first string after applying center() function: ------------python------------<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given second string = \"GOODMORNING\"\r\nGiven length = 17\r\nGiven character = '#'<\/pre>\n

Output:<\/strong><\/p>\n

The above given second string is : GOODMORNING\r\nThe given second string after applying center() function: ###GOODMORNING###<\/pre>\n

Program for center() Function in Python<\/h2>\n