TIM*_*MEX 536 python capitalize capitalization
s = 'the brown fox'
Run Code Online (Sandbox Code Playgroud)
......在这里做点什么......
s 应该 :
'The Brown Fox'
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?
Mar*_*off 893
.title()字符串的方法(ASCII或Unicode很好)这样做:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
Run Code Online (Sandbox Code Playgroud)
但是,请注意带有嵌入式撇号的字符串,如文档中所述.
该算法使用简单的与语言无关的单词定义作为连续字母组.该定义在许多情况下起作用,但它意味着收缩和占有的撇号形成单词边界,这可能不是所希望的结果:
Run Code Online (Sandbox Code Playgroud)>>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk"
小智 179
该.title()方法不能很好地工作,
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
Run Code Online (Sandbox Code Playgroud)
尝试string.capwords()方法,
import string
string.capwords("they're bill's friends from the UK")
>>>"They're Bill's Friends From The Uk"
Run Code Online (Sandbox Code Playgroud)
使用str.split()将参数拆分为单词,使用str.capitalize()对每个单词进行大写,并使用str.join()连接大写单词.如果可选的第二个参数sep不存在或者None,则用空格字符替换单个空格并删除前导和尾随空格,否则sep用于拆分和连接单词.
ste*_*eha 100
仅仅因为这种事情对我来说很有趣,这里还有两个解决方案.
分成单词,从分组中初始化每个单词,然后重新加入.这将改变将单词分隔成单个空白区域的空白区域,无论它是什么.
s = 'the brown fox'
lst = [word[0].upper() + word[1:] for word in s.split()]
s = " ".join(lst)
Run Code Online (Sandbox Code Playgroud)
编辑:当我编写上面的代码时,我不记得我在想什么,但是没有必要建立一个明确的列表; 我们可以使用生成器表达式以懒惰的方式执行它.所以这是一个更好的解决方案:
s = 'the brown fox'
s = ' '.join(word[0].upper() + word[1:] for word in s.split())
Run Code Online (Sandbox Code Playgroud)
使用正则表达式匹配字符串的开头,或分隔单词的空格,以及单个非空白字符; 使用括号标记"匹配组".编写一个带有匹配对象的函数,并返回未更改的空格匹配组和大写的非空白字符匹配组.然后re.sub()用来替换图案.这个没有第一个解决方案的标点符号问题,也不像我的第一个解决方案那样重做白色空间.这个产生最好的结果.
import re
s = 'the brown fox'
def repl_func(m):
"""process regular expression match groups for word upper-casing problem"""
return m.group(1) + m.group(2).upper()
s = re.sub("(^|\s)(\S)", repl_func, s)
>>> re.sub("(^|\s)(\S)", repl_func, s)
"They're Bill's Friends From The UK"
Run Code Online (Sandbox Code Playgroud)
我很高兴我研究了这个答案.我不知道re.sub()可以采取功能!您可以在内部re.sub()进行非平凡处理以产生最终结果!
alj*_*gom 15
以下是对不同方法的总结,它们适用于所有这些输入:
"" => ""
"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"
Run Code Online (Sandbox Code Playgroud)
- 最简单的解决方案是将句子分成单词并将第一个字母大写,然后将其连接在一起:
# 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(' '))
Run Code Online (Sandbox Code Playgroud)
- 如果您不想先将输入字符串拆分为单词,并使用花式生成器:
# 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)) )
Run Code Online (Sandbox Code Playgroud)
- 或者不导入itertools:
def cap_sentence(s):
return ''.join( (c.upper() if i == 0 or s[i-1] == ' ' else c) for i, c in enumerate(s) )
Run Code Online (Sandbox Code Playgroud)
- 或者你可以使用正则表达式,来自steveha的回答:
# 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)
Run Code Online (Sandbox Code Playgroud)
现在,这些是已发布的其他一些答案,如果我们使用单词的定义作为句子的开头或空格后的任何内容,那么它们将无法正常工作的输入:
return s.title()
# Undesired outputs:
"foO baR" => "Foo Bar"
"foo's bar" => "Foo'S Bar"
"foo's1bar" => "Foo'S1Bar"
"foo 1bar" => "Foo 1Bar"
Run Code Online (Sandbox Code Playgroud)
return ' '.join(w.capitalize() for w in s.split())
# or
import string
return string.capwords(s)
# Undesired outputs:
"foO baR" => "Foo Bar"
"foo bar" => "Foo Bar"
Run Code Online (Sandbox Code Playgroud)
使用''进行拆分将修复第二个输出,但是capwords()仍然无法用于第一个输出
return ' '.join(w.capitalize() for w in s.split(' '))
# or
import string
return string.capwords(s, ' ')
# Undesired outputs:
"foO baR" => "Foo Bar"
Run Code Online (Sandbox Code Playgroud)
小心多个空格
return ' '.join(w[0].upper() + w[1:] for w in s.split())
# Undesired outputs:
"foo bar" => "Foo Bar"
Run Code Online (Sandbox Code Playgroud)
Kon*_*rin 14
@jibberia anwser的复制粘贴就绪版本:
def capitalize(line):
return ' '.join(s[:1].upper() + s[1:] for s in line.split(' '))
Run Code Online (Sandbox Code Playgroud)
小智 11
当解决方案简单而安全时,为什么要使用连接和循环使您的生活复杂化?
这样做:
string = "the brown fox"
string[0].upper()+string[1:]
Run Code Online (Sandbox Code Playgroud)
jib*_*ria 10
如果str.title()不适合您,请自行执行大写操作.
一内胆:
>>> ' '.join([s[0].upper() + s[1:] for s in "they're bill's friends from the UK".split(' ')])
"They're Bill's Friends From The UK"
Run Code Online (Sandbox Code Playgroud)
清楚的例子:
input = "they're bill's friends from the UK"
words = input.split(' ')
capitalized_words = []
for word in words:
title_case_word = word[0].upper() + word[1:]
capitalized_words.append(title_case_word)
output = ' '.join(capitalized_words)
Run Code Online (Sandbox Code Playgroud)
小智 8
如果您使用 .title() 方法,那么 ' 后面的字母也会变成大写。像这样:
>>> "hello world's".title()
"Hello World'S"
Run Code Online (Sandbox Code Playgroud)
为了避免这种情况,请使用字符串库中的 capwords 函数。像这样:
>>> import string
>>> string.capwords("hello world's")
"Hello World's"
Run Code Online (Sandbox Code Playgroud)
正如马克指出的,你应该使用.title():
"MyAwesomeString".title()
Run Code Online (Sandbox Code Playgroud)
但是,如果想在 Django 模板中将第一个字母设为大写,您可以使用以下命令:
{{ "MyAwesomeString"|title }}
Run Code Online (Sandbox Code Playgroud)
或者使用变量:
{{ myvar|title }}
Run Code Online (Sandbox Code Playgroud)
如果只想要第一个字母:
>>> 'hello world'.capitalize()
'Hello world'
Run Code Online (Sandbox Code Playgroud)
但是要大写每个单词:
>>> 'hello world'.title()
'Hello World'
Run Code Online (Sandbox Code Playgroud)
如果访问[1:],空字符串将引发错误,因此我会使用:
def my_uppercase(title):
if not title:
return ''
return title[0].upper() + title[1:]
Run Code Online (Sandbox Code Playgroud)
仅限大写第一个字母.
尽管所有的答案都已经令人满意,但我将尝试将两个额外的案例与之前的所有案例一起讨论。
如果空间不均匀并且您想保持相同
string = hello world i am here.
Run Code Online (Sandbox Code Playgroud)
如果所有字符串都不是从字母开始
string = 1 w 2 r 3g
Run Code Online (Sandbox Code Playgroud)
在这里你可以使用这个:
def solve(s):
a = s.split(' ')
for i in range(len(a)):
a[i]= a[i].capitalize()
return ' '.join(a)
Run Code Online (Sandbox Code Playgroud)
这会给你:
output = Hello World I Am Here
output = 1 W 2 R 3g
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
495260 次 |
| 最近记录: |