Python | Lowercase first character of String
The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the first character of string to lowercase. Let’s discuss certain ways in which this can be performed.
Method #1 : Using string slicing + lower()
This task can easily be performed using the lower method which lowercases the characters provided to it and slicing can be used to add the remaining string after the lowercase first character.
# Python3 code to demonstrate working of # Lowercase first character of String # Using lower() + string slicing # initializing string test_str = "GeeksforGeeks" # printing original string print ( "The original string is : " + str (test_str)) # Using lower() + string slicing # Lowercase first character of String res = test_str[ 0 ].lower() + test_str[ 1 :] # printing result print ( "The string after lowercasing initial character : " + str (res)) |
The original string is : GeeksforGeeks The string after lowercasing initial character : geeksforGeeks
Method #2 : Using lambda + string slicing + lower()
The recipe of lambda function has to be added if we need to perform the task of handling None values or empty strings as well, and this becomes essential to handle edge cases.
# Python3 code to demonstrate working of # Lowercase first character of String # Using lower() + string slicing + lambda # initializing string test_str = "GeeksforGeeks" # printing original string print ( "The original string is : " + str (test_str)) # Using lower() + string slicing + lambda # Lowercase first character of String res = lambda test_str: test_str[: 1 ].lower() + test_str[ 1 :] if test_str else '' # printing result print ( "The string after lowercasing initial character : " + str (res(test_str))) |
The original string is : GeeksforGeeks The string after lowercasing initial character : geeksforGeeks