简体   繁体   English

索引中的基本字符串切片

[英]Basic string slicing from indices

I will state the obvious that I am a beginner.我会 state 很明显我是初学者。 I should also mention that I have been coding in Zybooks, which affects things.我还应该提到我一直在 Zybooks 中编码,这会影响事情。 My textbook hasn't helped me much我的教科书对我帮助不大

I tried sub_lyric= rhyme_lyric[: ]我试过 sub_lyric= rhyme_lyric[: ]

Zybooks should be able to input an index number can get only that part of the sentence but my book doesnt explain how to do that. Zybooks 应该能够输入索引号,只能获取句子的那部分,但我的书没有解释如何做到这一点。 If it throws a [4:7] then it would output cow.如果它抛出 [4:7] 那么它将是 output 奶牛。 Hopefully I have exolained everything well.希望我已经很好地解释了一切。

问题截图

You need to set there:你需要在那里设置:

sub_lyric = rhyme_lyric[start_index:end_index]

The string is as a sequence of characters and you can use string slicing to extract any sub-text from the main one.字符串是一个字符序列,您可以使用字符串切片从主文本中提取任何子文本。 As you have observed:正如您所观察到的:

sub_lyric = rhyme_lyric[:]

will copy the entire content of rhyme_lyric to sub_lyric .rhyme_lyric的全部内容复制到sub_lyric

To select only a portion of the text, specify the start_index (strings start with index 0) to end_index (not included).到 select 仅文本的一部分,指定start_index (以索引 0 开头的字符串)到end_index (不包括在内)。

sub_lyric = rhyme_lyric[4:7]

will extract characters in rhyme_lyric from position 4 (included) to position 7 (not included) so the result will be cow .将从 position 4 (包括)到 position 7 (不包括)中提取rhyme_lyric中的字符,因此结果将是cow

You can check more on string slicing here: Python 3 introduction您可以在此处查看有关字符串切片的更多信息: Python 3 简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM