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

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

    Maybe I missed it, but I couldn't find a complete answer on this page to the original question(s) because variables are not further discussed here. So I had to go on searching.

    Since I'm not yet allowed to comment, let me add my conclusion here. I'm sure I was not the only one interested in it when accessing this page:

     >>>myString = 'Hello World'
     >>>end = 5
    
     >>>myString[2:end]
     'llo'
    

    If you leave the first part, you get

     >>>myString[:end]
     'Hello' 
    

    And if you left the : in the middle as well you got the simplest substring, which would be the 5th character (count starting with 0, so it's the blank in this case):

     >>>myString[end]
     ' '
    

提交回复
热议问题