Categories
python string substring

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

2530

I want to get a new string from the third character to the end of the string, e.g. myString[2:end]. If omitting the second part means ’till the end’, and if you omit the first part, does it start from the start?

1

3591

>>> 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.

0

    469

    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.

    4

    • 26

      @mtahmed absolutely related to question. What if you wanted to substring by selecting alternate characters from the string? That would be my_string[::2]

      – Endophage

      Feb 12, 2013 at 17:59

    • I think it’s more likely you wanted to mention the third parameter to slice. Needing to get every other character from a string may be an important use case somewhere, but I’ve never had to do it. Not that there’s anything wrong with wanting to show off what you know — what’s the point of knowing things if you can’t do that. 🙂 But the case for relevance to the question is overstated.

      Dec 22, 2017 at 11:03

    • 2

      Sure, the specific example of selecting alternate characters may not be relevant to the question, but understanding there is a 3rd parameter to slicing very much is relevant and the simple examples serve to illustrate how it works. The Python community also has a great history of educating new members in a friendly way 🙂

      – Endophage

      Jan 4, 2018 at 18:47

    • It is clear that if you put some_string[::-1] you got back, the string in reverse order. However, I really don’t understand what you do in this case with the other numbers? Ex.: test_string[5:1:-1] – will result a totally different way that I expect. How the first and second numbers will effect the string if the third number is “-1” ?

      – Zoliqa

      Mar 21, 2021 at 23:03

    155

    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]
    

    5

    • 88

      The beginners should learn the pythonic way when moving to python, not stick to other language habits

      May 29, 2013 at 13:58

    • 3

      And just for completeness, Java is like Python in that the String.substring() method takes start and one-past-end. This one just bit me hard, I had assumed it was length like every other substring function in the world.

      – PhilHibbs

      Jan 10, 2019 at 13:34

    • 9

      A (probably) more pythonic way to do that is s[beginning:][:length]

      – victortv

      Mar 12, 2019 at 22:47

    • 2

      As someone who began with Python instead of [dirty word]-languages like PHP, I think Python is much more simple and intuitive with its string[beginning:end]. Length generally isn’t relevant.

      – Gloweye

      Oct 9, 2019 at 9:00

    • 1

      @PhilHibbs “Like every other substring function” is rather too strong a statement, since there are at least two other common ways to interpret substring arguments. One is (start, length) and the other is (start, end). Python’s (start, end+1) is admittedly unusual, but fits well with the way other things in Python work.

      – AndyB

      Jan 29 at 22:16