Python:修改字符串的一部分
我将输入一个字符串,它是一组连续的字母,并将其拆分为一个句子。问题是,作为初学者,我不知道如何修改字符串,使其仅大写第一个字母,并将其他字母转换为小写。我知道字符串.lower,但它将所有内容转换为小写。有什么想法吗Python:修改字符串的一部分,python,string,Python,String,我将输入一个字符串,它是一组连续的字母,并将其拆分为一个句子。问题是,作为初学者,我不知道如何修改字符串,使其仅大写第一个字母,并将其他字母转换为小写。我知道字符串.lower,但它将所有内容转换为小写。有什么想法吗 # This program asks user for a string run together # with each word capitalized and gives back the words # separated and only the first wo
# This program asks user for a string run together
# with each word capitalized and gives back the words
# separated and only the first word capitalized
import re
def main():
# ask the user for a string
string = input( 'Enter some words each one capitalized, run together without spaces ')
for ch in string:
if ch.isupper() and not ch.islower():
newstr = re.sub('[A-Z]',addspace,string)
print(newstr)
def addspace(m) :
return ' ' + m.group(0)
#call the main function
main()
不相关的例子。要仅大写第一个字母,可以执行以下操作:
>>> s = 'hello'
>>> s = s[0].upper()+s[1:]
>>> print s
Hello
>>> s = 'heLLO'
>>> s = s[0].upper()+s[1:]
>>> print s
HeLLO
对于整个字符串,您可以
>>> s = 'what is your name'
>>> print ' '.join(i[0].upper()+i[1:] for i in s.split())
What Is Your Name
[编辑]
您还可以执行以下操作:
>>> s = 'Hello What Is Your Name'
>>> s = ''.join(j.lower() if i>0 else j for i,j in enumerate(s))
>>> print s
Hello what is your name
不相关的例子。要仅大写第一个字母,可以执行以下操作:
>>> s = 'hello'
>>> s = s[0].upper()+s[1:]
>>> print s
Hello
>>> s = 'heLLO'
>>> s = s[0].upper()+s[1:]
>>> print s
HeLLO
对于整个字符串,您可以
>>> s = 'what is your name'
>>> print ' '.join(i[0].upper()+i[1:] for i in s.split())
What Is Your Name
[编辑]
您还可以执行以下操作:
>>> s = 'Hello What Is Your Name'
>>> s = ''.join(j.lower() if i>0 else j for i,j in enumerate(s))
>>> print s
Hello what is your name
您可以使用:
返回第一个字符大写且
其余部分小写
您可以使用:
返回第一个字符大写且
其余部分小写
如果您只想大写句子的开头(并且您的字符串有多个句子),可以执行以下操作:
>>> sentences = "this is sentence one. this is sentence two. and SENTENCE three."
>>> split_sentences = sentences.split('.')
>>> '. '.join([s.strip().capitalize() for s in split_sentences])
'This is sentence one. This is sentence two. And sentence three. '
如果不想更改句子开头字母的大小写,则可以定义自己的大写函数:
>>> def my_capitalize(s):
if s: # check that s is not ''
return s[0].upper() + s[1:]
return s
然后:
>>> '. '.join([my_capitalize(s.strip()) for s in split_sentences])
'This is sentence one. This is sentence two. And SENTENCE three. '
如果您只想大写句子的开头(并且您的字符串有多个句子),可以执行以下操作:
>>> sentences = "this is sentence one. this is sentence two. and SENTENCE three."
>>> split_sentences = sentences.split('.')
>>> '. '.join([s.strip().capitalize() for s in split_sentences])
'This is sentence one. This is sentence two. And sentence three. '
如果不想更改句子开头字母的大小写,则可以定义自己的大写函数:
>>> def my_capitalize(s):
if s: # check that s is not ''
return s[0].upper() + s[1:]
return s
然后:
>>> '. '.join([my_capitalize(s.strip()) for s in split_sentences])
'This is sentence one. This is sentence two. And SENTENCE three. '
字符串中的第一个单词已大写。我希望字符串中的所有其他单词都是uncapitalized@Maggi3339336然后,您最好还是使用
capitalize()
——它清晰易读。字符串中的第一个单词已经大写了。我希望字符串中的所有其他单词都是uncapitalized@Maggi3339336那么,您最好还是使用capitalize()
-它清晰易读。我将答案扩展为仅大写第一个单词和小写所有其他单词。第一个单词或第一个单词的第一个字符?我将答案扩展为仅大写第一个单词和小写所有其他单词。第一个单词或第一个单词的第一个字符?为什么不使用str.capitalize()?@sjcipher,因为alecxe做到了。我们最好提供一系列选项供选择,而不是所有选项都放在同一个最佳答案上。好的,谢谢,很高兴知道所有这些选项。非常感谢。为什么不使用str.capitalize()?@sjcipher,因为alecxe就是这么做的。我们最好提供一系列选项供选择,而不是所有选项都放在同一个最佳答案上。好的,谢谢,很高兴知道所有这些选项。谢谢你。