s = 'the brown fox'
...在这里做点什么……
S应为:
'The Brown Fox'
最简单的方法是什么?
s = 'the brown fox'
...在这里做点什么……
S应为:
'The Brown Fox'
最简单的方法是什么?
当前回答
.title()方法不会在所有测试用例中工作,因此将.capitalize(), .replace()和.split()一起使用是将每个单词的第一个字母大写的最佳选择。
def caps(y):
k=y.split()
for i in k:
y=y.replace(i,i.capitalize())
return y
其他回答
大写的单词…
str = "this is string example.... wow!!!";
print "str.title() : ", str.title();
@Gary02127评论,下面的解决方案适用于带有撇号的标题
import re
def titlecase(s):
return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(), s)
text = "He's an engineer, isn't he? SnippetBucket.com "
print(titlecase(text))
字符串的.title()方法(ASCII或Unicode都可以)这样做:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
但是,请注意带有内嵌撇号的字符串,如文档中所述。
该算法使用一个简单的独立于语言的单词定义作为连续的字母组。这个定义在很多情况下都适用,但它意味着缩略词和所有格中的撇号形成了单词边界,这可能不是理想的结果: >>> "他们是比尔来自英国的朋友".title() “他们是比尔的英国朋友”
这里总结了不同的方法,以及一些需要注意的陷阱
它们将适用于所有这些输入:
"" => ""
"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"
.title()方法不会在所有测试用例中工作,因此将.capitalize(), .replace()和.split()一起使用是将每个单词的第一个字母大写的最佳选择。
def caps(y):
k=y.split()
for i in k:
y=y.replace(i,i.capitalize())
return y
以防你想裁员
# Assuming you are opening a new file
with open(input_file) as file:
lines = [x for x in reader(file) if x]
# for loop to parse the file by line
for line in lines:
name = [x.strip().lower() for x in line if x]
print(name) # Check the result