How do I get a substring of a string in Python?

前端 未结 13 2144
我寻月下人不归
我寻月下人不归 2020-11-22 00:32

Is there a way to substring a string in Python, to get a new string from the third character to the end of the string?

Maybe like myString[2:end]?

13条回答
  •  不要未来只要你来
    2020-11-22 01:21

    Just for completeness as nobody else has mentioned it. The third parameter to an array slice is a step. So reversing a string is as simple as:

    some_string[::-1]
    

    Or selecting alternate characters would be:

    "H-e-l-l-o- -W-o-r-l-d"[::2] # outputs "Hello World"
    

    The ability to step forwards and backwards through the string maintains consistency with being able to array slice from the start or end.

提交回复
热议问题