转换一个字符串,使第一个字母为大写,而everythingelse为小写

时间:2012-11-29 21:38:24

标签: python

  

可能重复:
  How to capitalize the first letter of each word in a string (Python)?

是否有选项可以转换字符串,使得第一个字母为大写,而everythingelse为小写....如下所示..我知道有上部和下部用于转换为大写和小写....

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

3 个答案:

答案 0 :(得分:15)

只需使用str.title()

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

In [74]: a.title(), b.title()
Out[74]: ('Italic', 'Italic')
<{1}}上的

help():

str.title()

答案 1 :(得分:11)

是的,只需使用capitalize()方法。

例如:

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

标题实际上会将每个单词大写,就像它是标题一样。大写只会将字符串中的第一个字母大写。

答案 2 :(得分:0)

一种简单的方法:

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

要使它们小写(第一个字母除外):

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

我不知道是否有内置功能,但这应该可行。