Python字符串重新排列

2024-05-15 15:10:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python中重新排列字符串,但我不明白在这样做时哪些函数会有用?有人能帮我理解这一点吗:删除单词的第一个字母,并将该字母放在单词的末尾。然后在单词的末尾加上“IE”。如果有人能链接到可能使用的函数,我将不胜感激,这样我就可以了解它们是如何工作的

编辑:我曾试图用一个短语来完成这项工作,但我在将它放到ie中并放在单词末尾时遇到了问题。例如,HankTIE ouYIE将输出输入谢谢

以下是我所拥有的:

string = input("Please input a word: ")
def silly_encrypter(string):
    words = string.split()
    for words in string:
        first_letter= words[1:] + words[0]
        ie_end = first_letter + "IE"
        print (ie_end)

silly_encrypter(string)

Tags: 函数字符串inputstring字母单词ieend
1条回答
网友
1楼 · 发布于 2024-05-15 15:10:58

正如其他用户指出的,我强烈建议您阅读Python教程,该教程非常友好,包含许多示例,您可以在python控制台中试用

话虽如此,您可以利用字符串indexing加上concatenation来完成您想要的事情(教程中提到了这两件事):

remove the first letter of a word and place that letter at the end of the word:

s = "myString"

first_letter_at_the_end = s[1:] + s[0]
# If you print `first_at_the_end` you'll get: 'yStringm'

then append "IE" at the end

ie_at_the_end = first_letter_at_the_end + "IE"
# If you print `ie_at_the_end` you'll get: 'yStringmIE'

相关问题 更多 >