python编码的单词分隔符

时间:2013-10-25 23:35:07

标签: python word separator

所以问题是:

  

编写一个程序,接受一个句子,其中所有单词一起运行,但每个单词的第一个字符是大写。将句子转换为字符串,其中单词由空格分隔,只有第一个单词以大写字母开头。例如字符串“StopAndSmellTheRoses”。将被转换为“停止闻闻玫瑰。”

到目前为止,我对此代码感到困惑。

def main():

    #User enters a sentence
    my_string=input('enter a sentence: ')
    print(my_string.capitalize())

main()

4 个答案:

答案 0 :(得分:2)

您可以遍历字符串并每次为结果添加一个字符:

my_string = "StopAndSmellTheRoses"
i = 0
result = ""
for c in my_string:
    if c.isupper() and i > 0:
        result += " "
        result += c.lower()
    else:
        result += c
    i += 1

print result

当我们浏览字符串时,我们将为每个字符使用c,我们将使用i来跟踪字符串中的位置。

有两种可能性:它可以是大写字母(不包括第一个),也可以不是。

  • 在第一种情况下,我们将添加一个空格,并将该字符作为小写添加到结果中。这样可确保在句子中的每个大写字符之前插入空格。
  • 在第二种情况下,它是小写字符或句子开头的大写字符。我们不需要对这些做任何事情,我们马上就会添加它。

最后,每当我们完成一个字符(i)时,我们就会向i += 1添加一个,因为这意味着我们正确地知道了句子中的位置。

答案 1 :(得分:1)

欢迎来到SO!

执行此操作的一种方法是遍历字符串,逐个检查字符:

#You've learned how to iterate through something, right?
i = 0 #a counter
for c in my_string: #get the characters of my_string, one by one.
    if c.isupper():        #check if it's in upper case
        if i == 0: #if it's the first letter
            new_string += c   #let it be like the original
        else:
            new_string += ' '+.lower() #it's not the first letter, 
            #so add space, and lower the letter.
else:
    new_string += c    #else, only add the letter to the new string
i += 1

编辑添加了一项仔细检查以查看它是否是句子的第一个字母。更新了演示。

作为使用计数器的替代方法,您还可以使用内置函数enumerate,它返回索引和值的元组。

for i,c in enumerate(my_string): #get the characters of my_string, one by one.
    if c.isupper():        #check if it's in upper case
        if i == 0: #if it's the first letter
            new_string += c   #let it be like the original
        else:
            new_string += ' '+c.lower() #it's not the first letter, 
            #so add space, and lower the letter.
else:
    new_string += c    #else, only add the letter to the new string

<强>演示

>>> my_string = 'ImCool'
>>> new_string = ''
>>> i = 0 #a counter
>>> for c in my_string: #get the characters of my_string, one by one.
    if c.isupper():        #check if it's in upper case
        if i == 0: #if it's the first letter
            new_string += c   #let it be like the original
        else:
            new_string += ' '+.lower() #it's not the first letter, 
            #so add space, and lower the letter.
else:
    new_string += c    #else, only add the letter to the new string
    i += 1


>>> new_string
'Im cool'

希望这有帮助!

答案 2 :(得分:0)

你需要一点正则表达式。

import re
split = re.findall(r'[A-Z][a-z\.]+', 'HelloThisIsMyString.')

您还需要将它们连接在一起(插入空格)

' '.join(...)

并处理案例转换

' '.join(word.lower() for word in split)

(正如您已经做的那样,将第一个词大写)

' '.join(word.lower() for word in split).capitalize()

答案 3 :(得分:0)

看起来你有点困惑,如果你是Python的新手,这是预料之中的。我假设您从用户那里获取输入而不是输入函数。无论哪种方式,我会创建一个简单的函数,您可以插入用户输入。下面的功能将完成问题所在。

def sentenceSplitter(sentence):
result = ""
for i, x in enumerate(sentence):  #i is character index, x is the element
    if i == 0:
        result = result + x
    elif x.isupper() == False:  #if element is not uppercase, add it to the result
        result = result + x
    else:                       # Otherwise, add a space and lowercase the next letter
        result = result + " " +x.lower()
return(result)

重申一下,如果你打算打印出句子,你可以在函数后写下这个:

def main():

#User enters a sentence
my_string=input('enter a sentence: ')
print(sentenceSplitter(my_string))
main()

如果您仍然感到困惑,请随时提出任何问题。

相关问题