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.
Given a Python string, write a function that will capitalize the first letter and return it.
Ask Clarifying Questions
This prompt may be given during a job interview. In that case, ask the interviewer to clarify to be certain you understand the problem.
Here are some questions you might think about as we approach the problem:
- Question: Will the string always be present? I will never have to deal with a null or empty string?
- Answer: String will not always be present. Good to keep a look out for those edge cases though!
- Q: 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?
- A: First letter of the string itself.
- Q: 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?
- A: 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.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
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!
Handle Edge Cases First if Asked to Deal with Them.
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’s code that out now:
def capitalize_str(str): if str == None: # if str is None return "NoneType is not a string" elif len(str) > 0: #if str exists # what type of char is first character? If number, move to next letter, if letter, capitalize it. return str #this is temporary -- we'll change this in next step. else: #if str is empty return "undefined string" capitalize_str("hello")
Here, we’ve dealt with the edge cases where the string would be None or would have a length of 0. We haven’t yet done anything with the logic to return the string with the capitalized letter. We need to make sure it exists first.
Let’s look at how to check if the first character in the string is a letter. If it’s a number or special character, we need to move to the next character until we find the first letter. If it’s 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.
def capitalize_str(str): if str == None: # if str is None return "NoneType is not a string" elif len(str) > 0: #if str exists # what type of char is first character? If number, move to next letter, if letter, capitalize it. i = 0 while i < len(str): if str[i].isalpha(): return str[0:i] + str[i].upper() + str[i + 1:] else: i += 1 return "string does not have letters" else: #if str is empty return "undefined string" print(capitalize_str("christina"))
One of the string methods that exists in Python is
isalpha(). It checks to be certain that a character is a part of the alphabet. If it’s not, it moves to the next letter. When we get to the first letter character, we’ll return a concatenation of the left side of string, the uppercase letter, and the rest of the string.
There is the Python
capitalize() 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.
This is just one way to conquer this code challenge! I’m 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’re not clear on the prompt and to think about edge cases! If you can do that, you’ll be able to pass code challenges with hardly any problems. Good luck!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.