How to downcase the first character of a string?

PythonString

Python Problem Overview


There is a function to capitalize a string, I would like to be able to change the first character of a string to be sure it will be lowercase.

How can I do that in Python?

Python Solutions


Solution 1 - Python

One-liner which handles empty strings and None:

func = lambda s: s[:1].lower() + s[1:] if s else ''

>>> func(None)
>>> ''
>>> func('')
>>> ''
>>> func('MARTINEAU')
>>> 'mARTINEAU'

Solution 2 - Python

s = "Bobby tables"
s = s[0].lower() + s[1:]

Solution 3 - Python

def first_lower(s):
   if len(s) == 0:
      return s
   else:
      return s[0].lower() + s[1:]

print first_lower("HELLO")  # Prints "hELLO"
print first_lower("")       # Doesn't crash  :-)

Solution 4 - Python

Interestingly, none of these answers does exactly the opposite of capitalize(). For example, capitalize('abC') returns Abc rather than AbC. If you want the opposite of capitalize(), you need something like:

def uncapitalize(s):
  if len(s) > 0:
    s = s[0].lower() + s[1:].upper()
  return s

Solution 5 - Python

Simplest way:

>>> mystring = 'ABCDE'
>>> mystring[0].lower() + mystring[1:]
'aBCDE'
>>> 

Update

See this answer (by @RichieHindle) for a more foolproof solution, including handling empty strings. That answer doesn't handle None though, so here is my take:

>>> def first_lower(s):
   if not s: # Added to handle case where s == None
   return 
   else:
      return s[0].lower() + s[1:]

>>> first_lower(None)
>>> first_lower("HELLO")
'hELLO'
>>> first_lower("")
>>> 

Solution 6 - Python

No need to handle special cases (and I think the symmetry is more Pythonic):

def uncapitalize(s):
    return s[:1].lower() + s[1:].upper()

Solution 7 - Python

I'd write it this way:

def first_lower(s):
    if s == "":
        return s
    return s[0].lower() + s[1:]

This has the (relative) merit that it will throw an error if you inadvertently pass it something that isn't a string, like None or an empty list.

Solution 8 - Python

This duplicate post lead me here.

If you've a list of strings like the one shown below

l = ['SentMessage', 'DeliverySucceeded', 'DeliveryFailed']

Then, to convert the first letter of all items in the list, you can use

l = [x[0].lower() + x[1:] for x in l]

Output

['sentMessage', 'deliverySucceeded', 'deliveryFailed']

Solution 9 - Python

pip install pydash first.

import pydash  # pip install pydash

assert pydash.lower_first("WriteLine") == "writeLine"

https://github.com/dgilland/pydash

https://pydash.readthedocs.io/en/latest/

https://pypi.org/project/pydash/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNatimView Question on Stackoverflow
Solution 1 - PythonmartineauView Answer on Stackoverflow
Solution 2 - PythonJoshDView Answer on Stackoverflow
Solution 3 - PythonRichieHindleView Answer on Stackoverflow
Solution 4 - PythonAdrian McCarthyView Answer on Stackoverflow
Solution 5 - PythonManoj GovindanView Answer on Stackoverflow
Solution 6 - PythonDon O'DonnellView Answer on Stackoverflow
Solution 7 - PythonRobert RossneyView Answer on Stackoverflow
Solution 8 - PythonVan PeerView Answer on Stackoverflow
Solution 9 - PythonBaiJiFeiLongView Answer on Stackoverflow