Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a string such that the first letter is uppercase and everythingelse is lower case [duplicate]

Tags:

python

Possible Duplicate:
How to capitalize the first letter of each word in a string (Python)?

Is there an option to convert a string such that the first letter is uppercase and everythingelse is lower case....like below..i know there are upper and lower for converting to uppercase and lowercase....

string.upper() //for uppercase 
string.lower() //for lowercase
 string.lower() //for lowercase

INPUT:-italic,ITALIC

OUTPUT:-Italic

http://docs.python.org/2/library/stdtypes.html

like image 383
user1795998 Avatar asked Nov 29 '12 21:11

user1795998


People also ask

How will you convert a string to all lowercase and how will you capitalize the first letter of string?

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.

How do you capitalize the first character of a string to uppercase letter and lowercase all other characters?

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. Now, we would get the remaining characters of the string using the slice() function.

How do I make the first letter capital and rest lowercase in Excel?

In cell B2, type =PROPER(A2), then press Enter. This formula converts the name in cell A2 from uppercase to proper case. To convert the text to lowercase, type =LOWER(A2) instead. Use =UPPER(A2) in cases where you need to convert text to uppercase, replacing A2 with the appropriate cell reference.

How do you capitalize the first letter and lowercase the rest?

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. Copied!


3 Answers

Just use str.title():

In [73]: a, b = "italic","ITALIC"

In [74]: a.title(), b.title()
Out[74]: ('Italic', 'Italic')

help() on str.title():

S.title() -> string

Return a titlecased version of S, i.e. words start with uppercase
characters, all remaining cased characters have lowercase.
like image 81
Ashwini Chaudhary Avatar answered Oct 13 '22 01:10

Ashwini Chaudhary


Yeah, just use the capitalize() method.

eg:

x = "hello"
x.capitalize()
print x   #prints Hello

Title will actually capitalize every word as if it were a title. Capitalize will only capitalize the first letter in a string.

like image 20
SuperFamousGuy Avatar answered Oct 12 '22 23:10

SuperFamousGuy


A simple way to do it:

my_string = 'italic'
newstr = my_string[0]
newstr = newstr.upper()
my_string = newstr + my_string[1:]

To make them lowercase (except the first letter):

my_string= 'ITALIC'
newstr = my_string[1:]
newstr = newstr.lower()
my_string = my_string[0] + newstr

I don't know if there is a built in to do this, but this should work.

like image 39
Rushy Panchal Avatar answered Oct 12 '22 23:10

Rushy Panchal