Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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,我试着用一个短语来完成这项工作,但是我在把它放在单词末尾的ie上遇到了问题。例如,HankTIE ouYIE将输出输入谢谢 以下是我所拥有的: string=input("Please input a word: ") def silly_encrypter(string): strr = string.split() for strr in string: first_letter_at_the_end = strr[1:] + strr[0

我试着用一个短语来完成这项工作,但是我在把它放在单词末尾的ie上遇到了问题。例如,HankTIE ouYIE将输出输入谢谢

以下是我所拥有的:

string=input("Please input a word: ")
def silly_encrypter(string):
    strr = string.split()
    for strr in string:
        first_letter_at_the_end = strr[1:] + strr[0]
        ie_at_the_end = first_letter_at_the_end + "IE"
        print (ie_at_the_end)

silly_encrypter(string)
您可以这样做:

string=input("Please input a word: ")
def silly_encrypter(string):
    splitspace = string.split() # first split the string into spaces.
    for s in splitspace: # then looping over each element,
        strlist = list(s) # turn the element into a list
        strlist.append(strlist[0]) # first number to the last
        del strlist[0] # delete the first number
        strlist[0] = strlist[0].capitalize() # Capitalize the first letter
        strlist.append('IE') # add IE
        print(''.join(strlist), end=" ") # join the list

silly_encrypter(string)
您可以这样做:

string=input("Please input a word: ")
def silly_encrypter(string):
    splitspace = string.split() # first split the string into spaces.
    for s in splitspace: # then looping over each element,
        strlist = list(s) # turn the element into a list
        strlist.append(strlist[0]) # first number to the last
        del strlist[0] # delete the first number
        strlist[0] = strlist[0].capitalize() # Capitalize the first letter
        strlist.append('IE') # add IE
        print(''.join(strlist), end=" ") # join the list

silly_encrypter(string)

在阅读接受的答案后,我必须提供一个更干净的解决方案:

def silly_encryptor(phrase, suffix="IE"):
    new_phrase = []
    for word in phrase.split():
        new_phrase.append(word[1:]+word[:1]+suffix)
    return " ".join(new_phrase)

phrase = input("Please enter your phrase: ")
print (silly_encryptor(phrase))

在阅读接受的答案后,我必须提供一个更干净的解决方案:

def silly_encryptor(phrase, suffix="IE"):
    new_phrase = []
    for word in phrase.split():
        new_phrase.append(word[1:]+word[:1]+suffix)
    return " ".join(new_phrase)

phrase = input("Please enter your phrase: ")
print (silly_encryptor(phrase))

输入和输出示例是什么?输入:谢谢输出:hankTIE ouYIE我正在学习操纵字符串。输入和输出示例是什么?输入:谢谢输出:hankTIE ouYIE我正在学习操纵字符串。