Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to downcase the first character of a string?

Tags:

python

string

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?

like image 870
Natim Avatar asked Oct 01 '10 15:10

Natim


People also ask

How do you lowercase the first letter of a string?

To capitalize the first letter of a string and lowercase the rest, use the charAt() method to access the character at index 0 and capitalize it using the toUpperCase() method. Then concatenate the result with using the slice() method to to lowercase the rest of the string.

How do you Uncapitalize the first letter of a string in Python?

To convert the first letter of a string to lowercase, you can use string slicing and the Python lower() function. First, we get the first letter of a string and then use the lower() function to make it lowercase.

How do you capitalize the first letter in a string JavaScript?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you change the first letter of a string to lowercase in JavaScript?

The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.


1 Answers

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' 
like image 108
martineau Avatar answered Sep 22 '22 21:09

martineau