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

前端 未结 13 1903
我寻月下人不归
我寻月下人不归 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]
     ' '
    
    0 讨论(0)
  • 2020-11-22 01:19
    a="Helloo"
    print(a[:-1])
    

    In the above code, [:-1] declares to print from the starting till the maximum limit-1.

    OUTPUT :

    >>> Hello
    

    Note: Here a [:-1] is also the same as a [0:-1] and a [0:len(a)-1]

    a="I Am Siva"
    print(a[2:])
    

    OUTPUT:

    >>> Am Siva
    

    In the above code a [2:] declares to print a from index 2 till the last element.

    Remember that if you set the maximum limit to print a string, as (x) then it will print the string till (x-1) and also remember that the index of a list or string will always start from 0.

    0 讨论(0)
  • 2020-11-22 01:20

    I would like to add two points to the discussion:

    1. You can use None instead on an empty space to specify "from the start" or "to the end":

      'abcde'[2:None] == 'abcde'[2:] == 'cde'
      

      This is particularly helpful in functions, where you can't provide an empty space as an argument:

      def substring(s, start, end):
          """Remove `start` characters from the beginning and `end` 
          characters from the end of string `s`.
      
          Examples
          --------
          >>> substring('abcde', 0, 3)
          'abc'
          >>> substring('abcde', 1, None)
          'bcde'
          """
          return s[start:end]
      
    2. Python has slice objects:

      idx = slice(2, None)
      'abcde'[idx] == 'abcde'[2:] == 'cde'
      
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 01:23

    You've got it right there except for "end". It's called slice notation. Your example should read:

    new_sub_string = myString[2:]
    

    If you leave out the second parameter it is implicitly the end of the string.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
提交回复
热议问题