Convert first character of a string to Lowercase in Python

avatar

Borislav Hadzhiev

Last updated: Sep 7, 2022

banner

Photo from Unsplash

Convert first character of a string to Lowercase in Python #

To convert the first character of a string to lowercase:

  1. Access the character at index 0.
  2. Use the str.lower() method to convert the first character to lowercase.
  3. Append the rest of the string to the result.
main.py
def lowercase_first(string): if len(string) == 0: return '' return string[0].lower() + string[1:] print(lowercase_first('Bobby')) # 👉️ 'bobby' print(lowercase_first('Hadz')) # 👉️ 'hadz' print(lowercase_first('Com')) # 👉️ 'com' # ---------------------------------- my_str = 'Bobby' result = my_str[0].lower() + my_str[1:] print(result) # 👉️ bobby
Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

We used string slicing to append the rest of the string to the result.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

The slice string[1:] starts at the second character of the string and goes to the end of the string.

main.py
my_str = 'Bobby' result = my_str[0].lower() + my_str[1:] print(result) # 👉️ bobby

You can use the same approach if you need to convert the Nth character of a string to lowercase.

main.py
# 👇️ N based on index def lowercase_nth_char(string, index): return string[:index] + string[index].lower() + string[index + 1:] print(lowercase_nth_char('BOBBY', 2)) # 👉️ BObBY print(lowercase_nth_char('HADZ', 3)) # 👉️ HADz print(lowercase_nth_char('COM', 1)) # 👉️ CoM # ---------------------------------------------- # 👇️ N based on order of characters def lowercase_nth_char(string, n): return string[:n - 1] + string[n - 1].lower() + string[n:] print(lowercase_nth_char('BOBBY', 2)) # 👉️ BoBBY print(lowercase_nth_char('HADZ', 3)) # 👉️ HAdZ print(lowercase_nth_char('COM', 1)) # 👉️ cOM

The first function in the code snippet takes a string and a zero-based index and converts the character at the given index to lowercase.

The second function takes a string and the position of the character in the string and converts the character to lowercase.

If you need to convert the first letter of each word in a string to lowercase, use the str.split() function to split the string into a list of words.

main.py
def lowercase_first(string): if len(string) == 0: return '' return string[0].lower() + string[1:] my_str = 'Bobby Hadz Com' result = ' '.join(lowercase_first(item) for item in my_str.split()) print(result) # 👉️ bobby hadz com

The str.split() method splits the string into a list of substrings using a delimiter.

When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.
main.py
my_str = 'Bobby Hadz Com' print(my_str.split()) # 👉️ ['Bobby', 'Hadz', 'Com']

We used a generator expression to iterate over the list and used the lowercase_first function to convert the first character of each word to lowercase.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The last step is to join the results into a string.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used a string containing a space to join the words into a string with a space separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.