【问题标题】:Python 3: Slice a rhymePython 3:切韵
【发布时间】:2020-05-25 06:49:48
【问题描述】:

通过将 rhyme_lyric 从 start_index 切片到 end_index,将 sub_lyric 分配给 'cow'。给定程序的示例输出:

start_index = 4
end_index = 7
rhyme_lyric = 'The cow jumped over the moon.'
sub_lyric = rhyme_lyric''' Your solution goes here '''
print(sub_lyric)

【问题讨论】:

  • 不清楚您需要什么帮助。 SO 不是为 OP 提供完整的解决方案。它旨在帮助您了解遇到的困难。

标签: python-3.x computer-science


【解决方案1】:

这就是你要找的东西:

start_index = 4
end_index = 7
rhyme_lyric = 'The cow jumped over the moon.'
sub_lyric = rhyme_lyric [start_index: end_index]
print(sub_lyric)

【讨论】:

    【解决方案2】:

    这是你需要的

    start_index = 4
    end_index = 7
    rhyme_lyric = 'The cow jumped over the moon.'
    sub_lyric = rhyme_lyric[start_index : end_index]
    print(sub_lyric)
    

    python中的字符串被认为是列表和数组,它的第i个字符可以被访问为string[i]。此外,rhyme_lyric 包含单词cow,并且对其进行切片以提取“cow”。您可以在https://www.geeksforgeeks.org/interesting-facts-about-strings-in-python-set-2/

    阅读有关切片的更多信息

    【讨论】:

      【解决方案3】:

      我一直在检查这里给出的所有解决方案并进行了简单的分析。 对我有用的解决方案是使用以下代码行:

      sub_lyric = rhyme_lyric[start_index:end_index]

      【讨论】:

        【解决方案4】:

        start_index = int(输入())

        end_index = int(input())

        rhyme_lyric = '牛跳过了月亮。'

        sub_lyric = rhyme_lyric[start_index:end_index]

        打印(sub_lyric)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-25
          • 2023-03-11
          • 2014-07-18
          相关资源
          最近更新 更多