Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串转换为一个字符串,其中单词之间用空格分隔,只有第一个单词以大写字母开头_Python - Fatal编程技术网

Python 将字符串转换为一个字符串,其中单词之间用空格分隔,只有第一个单词以大写字母开头

Python 将字符串转换为一个字符串,其中单词之间用空格分隔,只有第一个单词以大写字母开头,python,Python,我正在尝试制作一个脚本,该脚本将接受一个字符串作为输入,其中所有单词一起运行,但每个单词的第一个字符都是大写。它应该将字符串转换为一个字符串,其中单词之间用空格分隔,并且只有第一个单词以大写字母开头 例如,输入: "StopWhateverYouAreDoingInterestingIDontCare" 预期产出: "Stop whatever you are doing interesting I dont care" Stop whatever y

我正在尝试制作一个脚本,该脚本将接受一个字符串作为输入,其中所有单词一起运行,但每个单词的第一个字符都是大写。它应该将字符串转换为一个字符串,其中单词之间用空格分隔,并且只有第一个单词以大写字母开头

例如,输入:

"StopWhateverYouAreDoingInterestingIDontCare"
预期产出:

"Stop whatever you are doing interesting I dont care"
Stop whatever you are doing interesting I dont care 
以下是我到目前为止写的一篇:

string_input = "StopWhateverYouAreDoingInterestingIDontCare"

def organize_string():
   start_sentence = string_input[0] 
   index_of_i = string_input.index("I")
   for i in string_input[1:]: 
      if i == "I" and string_input[index_of_i + 1].isupper(): 
           start_sentence += ' ' + i 
      elif i.isupper():      
           start_sentence += ' ' + i.lower()
      else: 
           start_sentence += i
return start_sentence
虽然这涉及到一些部分,但我正在努力区分字母I是单个还是一个完整的单词。以下是我的输出:

"Stop whatever you are doing interesting i dont care"
Single I需要大写,而单词interest中的I应该小写


我将非常感谢所有的帮助

把句子分成几个单词。如果你在这个列表中找到I这个词,就别管它了。别管第一个字。所有其他的单词,您都可以转换成小写。

您必须使用如下字符串操作:

输出=字符串\输入[0] 对于字符串中的l_输入[1:]: 如果l.islower: 新的_s+=l 其他: 新_s+=''''低+l 打印输出 我对你的程序做了一些修改。我用计数器检查索引位置。我得到了您的预期输出:

"Stop whatever you are doing interesting I dont care"
Stop whatever you are doing interesting I dont care 

在本例中,正则表达式就可以了

import re
s = "StopWhateverYouAreDoingInterestingIDontCare"
t = re.sub(r'(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z])', ' ', s)
印刷品:

Stop whatever you are doing interesting I dont care
更新3:我看了一下为什么你的代码没有正确地形成我应该首先做的句子,而不是发布我自己的解决方案:-

下面是更正后的代码,其中包含一些关于更改的注释

string_input = "StopWhateverYouAreDoingInterestingIDontCare"

def organize_string():
   start_sentence = string_input[0] 
   #index_of_i = string_input.index("I")
   for i, char in enumerate(string_input[1:], start=1):
      if char == "I" and string_input[i + 1].isupper(): 
           start_sentence += ' ' + char          
      elif char.isupper():      
           start_sentence += ' ' + char.lower()
      else: 
           start_sentence += char
      
   return start_sentence

print(organize_string())
!。我注释掉了行index_of_I=string_input.indexI,因为它不做你需要的事情,它找到了第一个大写字母I的索引,而不是一个应该独立的I。它找到了感兴趣的I的索引,而不是字符串输入字符串中的I。这不是一个正确的说法

对于i,enumeratestring_input[1:]中的char,1表示从1开始的字符串中字母的索引,因为字符串_input[1:]从索引1开始,所以它们是同步的。i是字符串输入中字母的索引。 我将I改为char,以便更清楚地表明char就是角色。除了这些更改,代码与您编写的代码保持一致


现在,程序给出了正确的输出。

这里有一个解决方案,它利用重新打包来根据大写字符拆分字符串

进口稀土 text=停止正在执行的任何操作InterestingInCare 按大写字符拆分文本 text_splitted=re.split'[A-Z]',text 已拆分的打印文本 正如我们在分隔符下面的输出中看到的,保留了大写字符和前后文本。这意味着大写字符后面总是跟在单词的其余部分。第一个空字符串源自第一个大写字符,即第一个分隔符

打印输出 [ , ‘S’、‘top’, “W”,“hatever”, "Y","ou",, ‘A’、‘re’, “D”,“oing”, '我','有趣', “我”, ‘D’、‘ont’, “C”,“你是” ] 正如我们所看到的,第一个字符后面总是跟着单词的其余部分。把这两个词结合起来,我们就得到了分裂的词。这也使我们能够轻松地处理您的特殊情况下的I

删除第一个字符,因为如果第一个字符始终为上限,则该字符始终为空 text\u splitted=text\u splitted[1:] 结果=[] 对于0范围内的i,lentext_已拆分,2: word=text\u splitted[i]+text\u splitted[i+1] 如果i>0且单词!='我: word=word.lower result.appendword 结果=“”。joinresult 输出

Stop whatever you are doing interesting i dont care 

哦,我从来没有想过采取这种方法。非常感谢你的帮助,谢谢!!你的意思是在返回输出后拆分句子吗?是的,在插入空格后。更好的方法是进行拆分,而不是插入空格。你能从那里开始编码吗?我真的很感谢你的解答和解释,我每天都在学习很多新东西!谢谢更新:这个词的首字母不是小写的。这能回答你的问题吗。将除I之外的单个单词小写并将它们连接成一个字符串应该很简单。
Stop whatever you are doing interesting i dont care 
I hope this will work fine :-
string_input = "StopWhateverYouAreDoingInterestingIDontCare"
def organize_string():
    i=0
    while i<len(string_input):
        if string_input[i]==string_input[i].upper() and i==0 :
            print(' ',end='')
            print(string_input[i].upper(),end='')
        elif string_input[i]==string_input[i].upper() and string_input[i+1]==string_input[i+1].upper():
            print(' ',end='')
            print(string_input[i].upper(),end='')
        elif string_input[i]==string_input[i].upper() and i!=0:
            print(' ',end='')
            print(string_input[i].lower(),end='')
        if string_input[i]!=string_input[i].upper():
            print(string_input[i],end='')
        i=i+1
organize_string()