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

前端 未结 13 1909
我寻月下人不归
我寻月下人不归 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:06

    >>> x = "Hello World!"
    >>> x[2:]
    'llo World!'
    >>> x[:2]
    'He'
    >>> x[:-2]
    'Hello Worl'
    >>> x[-2:]
    'd!'
    >>> x[2:-2]
    'llo Worl'
    

    Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.

提交回复
热议问题