s = 'the brown fox'
...在这里做点什么……
S应为:
'The Brown Fox'
最简单的方法是什么?
s = 'the brown fox'
...在这里做点什么……
S应为:
'The Brown Fox'
最简单的方法是什么?
当前回答
一个快速函数适用于python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> capitalizeFirtChar = lambda s: s[:1].upper() + s[1:]
>>> print(capitalizeFirtChar('помните своих Предковъ. Сражайся за Правду и Справедливость!'))
Помните своих Предковъ. Сражайся за Правду и Справедливость!
>>> print(capitalizeFirtChar('хай живе вільна Україна! Хай живе Любовь поміж нас.'))
Хай живе вільна Україна! Хай живе Любовь поміж нас.
>>> print(capitalizeFirtChar('faith and Labour make Dreams come true.'))
Faith and Labour make Dreams come true.
其他回答
当解决方案简单而安全的时候,为什么你要用连接和for循环来使你的生活复杂化?
只要这样做:
string = "the brown fox"
string[0].upper()+string[1:]
我非常喜欢这个答案:
复制粘贴版本的@jibberia回答:
def capitalize(line):
return ' '.join([s[0].upper() + s[1:] for s in line.split(' ')])
但是我发送的一些行分离了一些空白的“字符,在尝试执行s[1:]时导致错误。可能有更好的方法,但我必须添加一个if len(s)>0,就像在
return ' '.join([s[0].upper() + s[1:] for s in line.split(' ') if len(s)>0])
这里总结了不同的方法,以及一些需要注意的陷阱
它们将适用于所有这些输入:
"" => ""
"a b c" => "A B C"
"foO baR" => "FoO BaR"
"foo bar" => "Foo Bar"
"foo's bar" => "Foo's Bar"
"foo's1bar" => "Foo's1bar"
"foo 1bar" => "Foo 1bar"
Splitting the sentence into words and capitalizing the first letter then join it back together: # Be careful with multiple spaces, and empty strings # for empty words w[0] would cause an index error, # but with w[:1] we get an empty string as desired def cap_sentence(s): return ' '.join(w[:1].upper() + w[1:] for w in s.split(' ')) Without splitting the string, checking blank spaces to find the start of a word def cap_sentence(s): return ''.join( (c.upper() if i == 0 or s[i-1] == ' ' else c) for i, c in enumerate(s) ) Or using generators: # Iterate through each of the characters in the string # and capitalize the first char and any char after a blank space from itertools import chain def cap_sentence(s): return ''.join( (c.upper() if prev == ' ' else c) for c, prev in zip(s, chain(' ', s)) ) Using regular expressions, from steveha's answer: # match the beginning of the string or a space, followed by a non-space import re def cap_sentence(s): return re.sub("(^|\s)(\S)", lambda m: m.group(1) + m.group(2).upper(), s)
现在,这些是其他一些被发布的答案,如果我们将一个单词定义为句子的开头或空格后的任何东西,输入就不会像预期的那样工作:
.title () 返回s.title () #不需要的输出: "foO baR" => "foO baR" "foo's bar" => "foo's bar" "foo's1bar" => "foo's1bar" "foo 1bar" => "foo 1bar"
.capitalize()或.capwords() 返回' '.join(w.r esize () for s.split()中的w) #或 进口的字符串 返回string.capwords(年代) #不需要的输出: "foO baR" => "foO baR" "foo bar" => "foo bar" 使用' '作为分割将修复第二个输出,但不能修复第一个输出 返回' '.join(w.r esize () for w in s.s split(' ')) #或 进口的字符串 返回字符串。大写字符(s, ' ') #不需要的输出: "foO baR" => "foO baR"
.upper () 注意使用多个空格,这可以通过使用' '进行分割来修复(如答案顶部所示) 返回' ' . join (w [0] .upper () + w (1:) w s.split ()) #不需要的输出: "foo bar" => "foo bar"
尽管所有的答案都是令人满意的,但我将尝试涵盖两个额外的情况以及所有前面的情况。
如果空间不均匀,你想保持不变
string = hello world i am here.
如果所有的字符串不是从字母开始
string = 1 w 2 r 3g
在这里你可以使用这个:
def solve(s):
a = s.split(' ')
for i in range(len(a)):
a[i]= a[i].capitalize()
return ' '.join(a)
这将给你:
output = Hello World I Am Here
output = 1 W 2 R 3g
复制粘贴版本的@jibberia回答:
def capitalize(line):
return ' '.join(s[:1].upper() + s[1:] for s in line.split(' '))