{"id":24166,"date":"2020-10-13T13:00:46","date_gmt":"2020-10-13T20:00:46","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=24166"},"modified":"2020-10-13T13:00:48","modified_gmt":"2020-10-13T20:00:48","slug":"python-capitalize","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-capitalize\/","title":{"rendered":"Python Fundamentals: Capitalize First Letter of String"},"content":{"rendered":"\n<p>There are many methods to manipulate strings in Python. If you must capitalize the first letter of a string in Python in a code challenge, how will you do it? This article talks about one way you can manipulate strings to meet that objective.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Prompt<\/h2>\n\n\n\n<p>Given a Python string, write a function that will capitalize the first letter and return it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Ask Clarifying Questions<\/h3>\n\n\n\n<p>This prompt may be given during a job interview. In that case, ask the interviewer to clarify to be certain you understand the problem.<br><\/p>\n\n\n\n<p>Here are some questions you might think about as we approach the problem:<br><br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Question<\/strong>: Will the string always be present? I will never have to deal with a null or empty string?<\/li><li><strong>Answer<\/strong>: <em>String will not always be present. Good to keep a look out for those edge cases though!<\/em><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Q<\/strong>: First letter. Does this mean just the first letter of the string itself or the first letter of each word in the string? How would you like me to define it?&nbsp;<\/li><li><strong>A<\/strong>: <em>First letter of the string itself.&nbsp;<\/em><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Q<\/strong>: Will the first character always be a letter? Do I have to deal with any cases where the first character in a string might be a number?&nbsp;<\/li><li><strong>A<\/strong>: <em>Good catch. The first letter may not always be the first character in a string. I would like for you to capitalize the first letter of the string.<\/em><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The Approach<\/h2>\n\n\n\n<p>There are a couple of different ways to approach this problem. There is more than one right way to do the problem. If you come up with a different way, great!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Handle Edge Cases First if Asked to Deal with Them.&nbsp;<\/h3>\n\n\n\n<p>Here, we need to handle what happens when we have a case where we have no string, an empty string, or the first character is not a letter. Let\u2019s code that out now:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def capitalize_str(str):\n if str == None: # if str is None\n   return &quot;NoneType is not a string&quot;\n elif len(str) &gt; 0: #if str exists\n   # what type of char is first character? If number, move to next letter, if letter, capitalize it.\n   return str #this is temporary -- we'll change this in next step.\n else: #if str is empty\n   return &quot;undefined string&quot;\n \ncapitalize_str(&quot;hello&quot;)<\/pre><\/div>\n\n\n\n<p>Here, we\u2019ve dealt with the edge cases where the string would be None or would have a length of 0. We haven\u2019t yet done anything with the logic to return the string with the capitalized letter. We need to make sure it exists first.&nbsp;<br><\/p>\n\n\n\n<p>Let\u2019s look at how to check if the first character in the string is a letter. If it\u2019s a number or special character, we need to move to the next character until we find the first letter. If it\u2019s a letter, we capitalize it and return the string with the capitalized letter. If we get to the end of the string and there is no letter, say so.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def capitalize_str(str):\n if str == None: # if str is None\n   return &quot;NoneType is not a string&quot;\n elif len(str) &gt; 0: #if str exists\n   # what type of char is first character? If number, move to next letter, if letter, capitalize it.\n   i = 0\n \n   while i &lt; len(str):\n     if str[i].isalpha():\n       return str[0:i] + str[i].upper() + str[i + 1:]\n     else:\n       i += 1\n    return &quot;string does not have letters&quot;\n else: #if str is empty\n   return &quot;undefined string&quot;\n \nprint(capitalize_str(&quot;christina&quot;))<\/pre><\/div>\n\n\n\n<p>One of the string methods that exists in Python is <code>isalpha()<\/code>. It checks to be certain that a character is a part of the alphabet. If it\u2019s not, it moves to the next letter. When we get to the first letter character, we\u2019ll return a concatenation of the left side of string, the uppercase letter, and the rest of the string.<br><\/p>\n\n\n\n<p>There is the Python <code>capitalize()<\/code> function that works because it converts the first character to uppercase, but it can not handle the edge case where the first character is not a letter. This is why we loop through using a while loop and then break the while loop intentionally on the first instance of finding a letter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This is just one way to conquer this code challenge! I\u2019m sure there are plenty of other ways that are just as good (if not better) than the way mentioned here. The goal is to learn to ask questions if you\u2019re not clear on the prompt and to think about edge cases! If you can do that, you\u2019ll be able to pass code challenges with hardly any problems. Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"There are many methods to manipulate strings in Python. If you must capitalize the first letter of a string in Python in a code challenge, how will you do it? This article talks about one way you can manipulate strings to meet that objective. The Prompt Given a Python string, write a function that will&hellip;","protected":false},"author":77,"featured_media":24167,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-24166","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":[]},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Capitalize First Letter | Career Karma<\/title>\n<meta name=\"description\" content=\"Learn how to write a simple algorithm to capitalize the first letter of a string in this article by Career Karma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Fundamentals: Capitalize First Letter of String\" \/>\n<meta property=\"og:description\" content=\"Learn how to write a simple algorithm to capitalize the first letter of a string in this article by Career Karma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-13T20:00:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-10-13T20:00:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"574\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"Python Fundamentals: Capitalize First Letter of String\",\"datePublished\":\"2020-10-13T20:00:46+00:00\",\"dateModified\":\"2020-10-13T20:00:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\"},\"wordCount\":632,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\",\"name\":\"Python Capitalize First Letter | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg\",\"datePublished\":\"2020-10-13T20:00:46+00:00\",\"dateModified\":\"2020-10-13T20:00:48+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"Learn how to write a simple algorithm to capitalize the first letter of a string in this article by Career Karma.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-capitalize\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg\",\"width\":1020,\"height\":574,\"caption\":\"Python written on a post it note\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-capitalize\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Fundamentals: Capitalize First Letter of String\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\/\/www.linkedin.com\/in\/cmvnk\"],\"url\":\"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Capitalize First Letter | Career Karma","description":"Learn how to write a simple algorithm to capitalize the first letter of a string in this article by Career Karma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-capitalize\/","og_locale":"en_US","og_type":"article","og_title":"Python Fundamentals: Capitalize First Letter of String","og_description":"Learn how to write a simple algorithm to capitalize the first letter of a string in this article by Career Karma.","og_url":"https:\/\/careerkarma.com\/blog\/python-capitalize\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-10-13T20:00:46+00:00","article_modified_time":"2020-10-13T20:00:48+00:00","og_image":[{"width":1020,"height":574,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"Python Fundamentals: Capitalize First Letter of String","datePublished":"2020-10-13T20:00:46+00:00","dateModified":"2020-10-13T20:00:48+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/"},"wordCount":632,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-capitalize\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/","url":"https:\/\/careerkarma.com\/blog\/python-capitalize\/","name":"Python Capitalize First Letter | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg","datePublished":"2020-10-13T20:00:46+00:00","dateModified":"2020-10-13T20:00:48+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"Learn how to write a simple algorithm to capitalize the first letter of a string in this article by Career Karma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-capitalize\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/10\/hitesh-choudhary-D9Zow2REm8U-unsplash.jpg","width":1020,"height":574,"caption":"Python written on a post it note"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-capitalize\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Fundamentals: Capitalize First Letter of String"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/24166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=24166"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/24166\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/24167"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=24166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=24166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=24166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}