You are currently viewing Python Lowercase First Letter

To convert the first letter/character of a string to a lowercase in Python, you can use the lower() method and concatenate it with the rest of the string. You can convert the first letter of a string to lowercase using many ways, for example, by using the string slicing + lower(), replace(), lambda + string slicing + lower(), ord(),chr(), and re.sub() functions. In this article, I will explain how to convert the first letter of a string to lowercase by using all these methods with examples.

Related: Replace the characters of the string with new ones.

1. Quick Examples of Lowercase First Letter

If you are in a hurry, below are some quick examples of converting the first letter of a string to lowercase the first letter of a string.


# Quick examples of lowercase first letter

# Convert first letter of a string to lowercase
# Initialize the string 
string = "Welcome to sparkbyexamples"

# Example 1: Using lower() + string slicing
result = string[0].lower() + string[1:]

# Example 2: Using replace() method without slicing
result = string.replace(string[0], string[0].lower(), 1)

# Example 3: Using lower() + string slicing + lambda
lowercase_first_char = lambda string: string[:1].lower() + string[1:] if string else ''
result = lowercase_first_char(string)

# Example 4: Using ord(),chr() functions 
result = ""
if(ord(string[0]) <= ord('Z')):
    result = chr(ord(string[0])+32)
    result += string[1:]
else:
    result = string

# Example 5: Using regular expression and the re.sub() function
def repl_func(match):
    return match.group(1).lower()
result = re.sub(r'^(.)', repl_func, string)

2. Get First Character of String as Lowercase Using Slicing + lower()

You can use Python lower() and string slicing together to get the first character of a string as lowercase and keep the rest of the string unchanged. For example, first, initialize a string variable string with the value "Welcome to sparkbyexamples" and print it.

Here, string slicing is used to extract the first character of the string (string[0]). Then apply lower() method over the string and convert its first character to lowercase. The remaining part of the string (string[1:]) is concatenated with the lowercase first character using the + operator. The resulting string is assigned to the result variable.

Related: How to capitalize the first letter of a string.


# Initialize the string 
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Using lower() + string slicing
# to convert first character of String to lowercase
lowercased = string[0].lower() + string[1:]
print("The string after converting its first letter to lowercase:\n", result)

Yields below output.

python lowercase first letter

In the output, you can see that the initial letter "W" has been successfully converted to lowercase, resulting in "welcome" the new first word of the string. The rest of the string remains unchanged.

3. Using replace() Method Without Slicing

To convert the first character of a string to lowercase you can use the Python replace() method without using slicing. In this program, this method replaces the first occurrence of the first character (string[0]) with its lowercase version (string[0].lower()) and the third argument ensures that only the first occurrence is replaced.


# Initialize string 
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Using replace() method without slicing
result = string.replace(string[0], string[0].lower(), 1)
print("The string after converting its first letter to lowercase:\n", result)

Yields the same output as above.

4. Using Lambda + String Slicing + lower()

You can also use a lambda function along with string slicing and the lower() method to convert the first character of a string to lowercase. For example, first, define a lambda function lowercase_first_char that takes a string s as input. The lambda function uses string slicing to extract the first character of the string (string[:1]), and then applies the lower() method to convert it to lowercase and, finally, concatenate it with the rest of the string (string[1:]). If the input string string is None or empty, the lambda function returns an empty string.

Then you call the lambda function lowercase_first_char with the original string string as an argument, and the resulting lowercase string with the first character lowercased is assigned to the result variable.


# Initialize string 
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Using lower() + string slicing + lambda
lowercase_first_char = lambda string: string[:1].lower() + string[1:] if string else ''
result = lowercase_first_char(string)
print("The string after converting its first letter to lowercase:\n", result)

Yields the same output as above.

5. Using ord(),chr() Functions

Another way to convert the first character of a string to lowercase use the ord() and chr() functions. For example, initialize a string variable string with the value "Welcome to sparkbyexamples".

Here, you can check if the Unicode value of the first character (ord(string[0])) is less than or equal to the Unicode value of 'Z' (ord('Z')). If the first character is uppercase, you can convert it to lowercase by adding 32 to its Unicode value using chr() and ord() functions. Then you can concatenate the lowercase first character with the rest of the string (string[1:]). If the first character is already lowercase or not a letter, you simply assign the original string to the result.


# Initialize string 
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Using ord(),chr() functions 
result = ""
if(ord(string[0]) <= ord('Z')):
    result = chr(ord(string[0])+32)
    result += string[1:]
else:
    result = string
print("The string after converting its first letter to lowercase:\n", result)

 Yields the same output as above.

6. Using re.sub() Function(Regular Expression)

You can use the re.sub() function to convert the first letter of a string to lowercase. For example, import the re module and initialize a string variable string with the value “Welcome to sparkbyexamples”. Here, first, define a replacement function repl_func(). It takes a match object match as input and returns the lowercased version of the matched character (match.group(1).lower()). The regular expression pattern r'^(.)' matches the first character of the string.

The re.sub() function is then used to perform the substitution. It replaces the first character of the string with the result of the repl_func() function. The modified string is assigned to the result variable.

Related: Convert integers to strings.


import re

# Initialize string 
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Using the re.sub() function
def repl_func(match):
    return match.group(1).lower()
result = re.sub(r'^(.)', repl_func, string)
print("The string after lowercasing initial letter:",result)

Yields the same output as above.

Conclusion

In this article, I have explained some of the Python methods like string slicing+lower(), replace(), lambda+string slicing+lower(), ord(),chr(), and the re.sub() functions and using these functions how we can convert the first character of a string to lowercase with well-defined examples.

Happy Learning !!