{"id":964,"date":"2019-12-16T06:07:04","date_gmt":"2019-12-16T06:07:04","guid":{"rendered":"https:\/\/www.askpython.com\/?p=964"},"modified":"2019-12-16T06:07:06","modified_gmt":"2019-12-16T06:07:06","slug":"python-string-capitalize-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-string-capitalize-function","title":{"rendered":"Python String capitalize() Function"},"content":{"rendered":"\n<p>String in Python has built-in functions for almost every action to be performed on a string. Python String capitalize() function is used to convert only the first character to the uppercase letter, rest all characters are lowercase. <\/p>\n\n\n\n<p><strong>Key Points :<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Return Type:<\/strong> String<\/li><li><strong>Parametric Values:<\/strong> No parameters can be parsed onto capitalize() function.<\/li><li>Converts only the first character of a string to uppercase.<\/li><li>It does not modify the original string. The modified string is saved with a new variable name.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong>  Given String &#8211; &#8220;Have a Nice Day&#8221;  Or  &#8220;Have A NICE DAY&#8221;   Or  &#8220;have a nice day&#8221;  Or  &#8220;Have a nice day&#8221;<\/p>\n\n\n\n<p>                  Capitalized String: &#8220;Have a nice day&#8221; (for all the above-given strings)<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color is-style-default\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax :<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_name.capitalize()\n<\/pre><\/div>\n\n\n<p>str_name here refers to the string to be capitalized. And, capitalize() is inbuilt string function in python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Example <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_name = &quot;hi there!&quot;\nnew_str = str_name.capitalize()\nprint(&#039;The New Capitalized String is &#039;,new_str)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> Hi there!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Different Cases :<\/h2>\n\n\n\n<p>Examples of different cases are given below &#8211;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Case 1: All the characters in a string are uppercase<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_name = &quot;HI THERE&quot;\nnew_str = str_name.capitalize()\nprint(&#039;The New Capitalized String is &#039;,new_str)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> Hi there!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Case 2: The first alphabet of every word in a string containing multiple words is uppercase<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_name = &quot;Hi There!&quot;\nnew_str = str_name.capitalize()\nprint(&#039;The New Capitalized String is &#039;,new_str)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> Hi there!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Case 3: Randomly any character in a string is uppercase<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_name = &quot;hI tHeRE!&quot;\nnew_str = str_name.capitalize()\nprint(&#039;The New Capitalized String is &#039;,new_str)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> Hi there!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Case 4: Non-alphanumeric or Numeric first character<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_name = &quot;! hi there&quot;\nnew_str = str_name.capitalize()\nprint(&#039;The New Capitalized String is &#039;,new_str)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> ! hi there!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/python-functions\">Pyth<\/a><a href=\"https:\/\/www.askpython.com\/python\/python-functions\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Python Functions (opens in a new tab)\">on Functions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>String in Python has built-in functions for almost every action to be performed on a string. Python String capitalize() function is used to convert only the first character to the uppercase letter, rest all characters are lowercase. Key Points : Return Type: String Parametric Values: No parameters can be parsed onto capitalize() function. Converts only [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-964","post","type-post","status-publish","format-standard","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=964"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/964\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}