{"id":3653,"date":"2022-12-01T21:01:46","date_gmt":"2022-12-01T21:01:46","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3653"},"modified":"2022-12-01T21:01:48","modified_gmt":"2022-12-01T21:01:48","slug":"python-string-center","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-center\/","title":{"rendered":"Python String center() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String center<\/p>\n<p>Now, we are going to learn about the center method. This method returns a centered string with the length of the given width. The padding is done with the specified character, which is given as an argument. Let\u2019s have a look at a simple example, which would demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3654 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-center-METHOD.jpg\" alt=\"\" width=\"642\" height=\"84\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-center-METHOD.jpg 642w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-center-METHOD-300x39.jpg 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String center() Method<\/h2>\n<p>As you can see in the above program, we have a string, and then we are trying to call the center method. We have provided two arguments for the center method. The first one is the width, which is going to be the length of the string with the padded characters. The another is the padding character. This argument is optional, and if not provided, the default one is considered as whitespace.<\/p>\n<p>Let\u2019s have a look at the output now, which would demonstrate the return value from the center method.<\/p>\n<p>&#8212;-GyaniPandit&#8212;&#8211;<\/p>\n<p>As you can see, we got a centered string, with a length of 20, and the padding is done with the specified character, which is a <strong>&#8216;-&#8216;<\/strong> in this case. If the padding character is not specified,\u00a0 it defaults to the whitespace. The center method returns a string, and the original string is not modified. Let\u2019s have a look at another example, where we would not give the padding character.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3655 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-center-METHOD-1.jpg\" alt=\"\" width=\"617\" height=\"72\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-center-METHOD-1.jpg 617w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-center-METHOD-1-300x35.jpg 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/p>\n<p>As you can see in the above program, we have not given the padding character this time, so, it defaults to the whitespace. Let\u2019s have a look at the output again, which gives a clearer idea about the output.<\/p>\n<p>The return value from the center method:\u00a0\u00a0\u00a0\u00a0 GyaniPandit<\/p>\n<p>As you can see, we got a centered string, with the padding done. So, as and when required, you can make use of the center method. Remember that the center method returns a centered string, and the padding is done with the specified padding character. If the padding character is not given as an argument to the center method, it defaults to whitespace.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String center Now, we are going to learn about the center method. This method returns a centered string with the length of the given width. The padding is done with the specified character, which is given as an argument. Let\u2019s have a look at a simple example, which would demonstrate the same thing. Python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3653","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3653"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3653\/revisions"}],"predecessor-version":[{"id":4179,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3653\/revisions\/4179"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}