{"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> 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 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program for casefold() Function center() Function in Python: The center () method will center align the string, using a specified character as the fill character (space is the default). Syntax: String.center(Length, character) Parameters: length: This is required. It is the length of the string returned. character: This …<\/p>\n
\ncenter() Function in Python:<\/strong><\/p>\nString.center(Length, character)<\/pre>\n
Given first string = \"python\"\r\nGiven length = 30 \r\nGiven character = '-'<\/pre>\n
The above given first string is : python\r\nThe given first string after applying center() function: ------------python------------<\/pre>\n
Given second string = \"GOODMORNING\"\r\nGiven length = 17\r\nGiven character = '#'<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first string as static input and store it in a variable.\r\ngvn_fststr = \"python\"\r\n# Give the length as static input and store it in another variable.\r\ngvn_lengt = 30\r\n# Give the character as\u00a0static input and store it in another variable.\r\ngvn_chactr = '-'\r\n# Apply center () method to the given string for the given length and character\r\n# which will center align the string.\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.center(gvn_lengt, gvn_chactr)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying center() function.\r\nprint(\"The given first string after applying center() function:\", rslt_str1)\r\n# Similarly do the same for other string and print the result string.\r\ngvn_scndstr = \"GOODMORNING\"\r\ngvn_lengt2 = 17\r\ngvn_chactr2 = '#'\r\nrslt_str2 = gvn_scndstr.center(gvn_lengt2, gvn_chactr2)\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying center() function:\", rslt_str2)\r\n<\/pre>\n
The above given first string is : python\r\nThe given first string after applying center() function: ------------python------------\r\nThe above given second string is : GOODMORNING\r\nThe given second string after applying center() function: ###GOODMORNING###<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first string as user input using the input() function and store it in a variable.\r\ngvn_fststr = input(\"Enter some random string = \")\r\n# Give the length as user input using the int(input()) function and store it in another variable.\r\ngvn_lengt = int(input(\"Enter some random number = \"))\r\n# Give the character as user input using the input() function and store it in another variable.\r\ngvn_chactr = input(\"Enter some random charcter = \")\r\n# Apply center () method to the given string for the given length and character\r\n# which will center align the string.\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.center(gvn_lengt, gvn_chactr)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying center() function.\r\nprint(\"The given first string after applying center() function:\", rslt_str1)\r\n# Similarly do the same for other string and print the result string.\r\ngvn_scndstr = input(\"Enter some random string = \")\r\ngvn_lengt2 = int(input(\"Enter some random number = \"))\r\ngvn_chactr2 = input(\"Enter some random charcter = \")\r\nrslt_str2 = gvn_scndstr.center(gvn_lengt2, gvn_chactr2)\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying center() function:\", rslt_str2)\r\n<\/pre>\n
Enter some random string = welcome\r\nEnter some random number = 16\r\nEnter some random charcter = !\r\nThe above given first string is : welcome\r\nThe given first string after applying center() function: !!!!welcome!!!!!\r\nEnter some random string = PYTHONPLATFORM\r\nEnter some random number = 8\r\nEnter some random charcter = *\r\nThe above given second string is : PYTHONPLATFORM\r\nThe given second string after applying center() function: PYTHONPLATFORM<\/pre>\n
\n