用于python编码的单词分隔符[关闭]

问题描述 投票:2回答:4

所以问题是:

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

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

def main():

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

main()
python word separator
4个回答
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
投票

欢迎来到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'

希望这可以帮助!


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()

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()

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

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.