search

Home  >  Q&A  >  body text

关于python切片的问题

如果定义一个以空格为结尾的字符串s,如果使用s[:-1],为何字符串最后的空格没有了,请问这是为何啊?

巴扎黑巴扎黑2875 days ago572

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:29:39

    s[:-1] will only slice to the penultimate element

    A subsequence of

    [start:stop] 中會切出 startstop-1.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:29:39

    All intervals in python are closed on the left and open on the right [m,n)

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:29:39

    -1 is the last element,
    If you don’t want to remove the leading and trailing spaces, should you use strip()

    ?

    reply
    0
  • Cancelreply