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

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

    Substr() normally (i.e. PHP and Perl) works this way:

    s = Substr(s, beginning, LENGTH)
    

    So the parameters are beginning and LENGTH.

    But Python's behaviour is different; it expects beginning and one after END (!). This is difficult to spot by beginners. So the correct replacement for Substr(s, beginning, LENGTH) is

    s = s[ beginning : beginning + LENGTH]
    

提交回复
热议问题