Python slice string

Filed Under: Python

Python string supports slicing to create substring. Note that Python string is immutable, slicing creates a new substring from the source string and original string remains unchanged.

Python slice string

Python slice string syntax is:


The slicing starts with the start_pos index (included) and ends at end_pos index (excluded). The step parameter is used to specify the steps to take from start to end index.

Python String slicing always follows this rule: s[:i] + s[i:] == s for any index ‘i’.

All these parameters are optional – start_pos default value is 0, the end_pos default value is the length of string and step default value is 1.

Let’s look at some simple examples of string slice function to create substring.

s = 'HelloWorld'





Note that since none of the slicing parameters were provided, the substring is equal to the original string.

Let’s look at some more examples of slicing a string.

s = 'HelloWorld'
first_five_chars = s[:5]

third_to_fifth_chars = s[2:5]



Note that index value starts from 0, so start_pos 2 refers to the third character in the string.

Reverse a String using Slicing

We can reverse a string using slicing by providing the step value as -1.

s = 'HelloWorld'
reverse_str = s[::-1]

Output: dlroWolleH

Let’s look at some other examples of using steps and negative index values.

s1 = s[2:8:2]

Output: loo

Here the substring contains characters from indexes 2,4 and 6.

s1 = s[8:1:-1]

Output: lroWoll

Here the index values are taken from end to start. The substring is made from indexes 1 to 7 from end to start.

python slice string

s1 = s[8:1:-2]

Output: lool

python slice string reverse

Python slice works with negative indexes too, in that case, the start_pos is excluded and end_pos is included in the substring.

s1 = s[-4:-2]

Output: or

python string slicing substring negative index

Python string slicing handles out of range indexes gracefully.

>>>s = 'Python'

That’s all for python string slice function to create substring.

You can checkout complete python script and more Python examples from our GitHub Repository.


  1. Aadi says:

    Plz tell me the slice of ‘HeloWr’

  2. malik says:

    TheData = [20, 3, 4,8,12, 99,4, 26 , 4 ]
    TheData1= TheData [ 0 : len(TheData) : 1 ]

    def InsertionData (TheData1):

    for Count in range(0, len(TheData1)):
    DataToInsert = TheData1(Count)
    Inserted = 0
    Nextvalue = Count – 1
    while (Nextvalue >= 0 and Inserted != 1):
    if DataToInsert < TheData1(Nextvalue):
    TheData1(Nextvalue + 1) == TheData1(Nextvalue)
    Nextvalue = Nextvalue -1
    TheData1(Nextvalue +1 ) == DataToInsert
    Inserted = 1

    def printarray(TheData1):
    for count in range(0, len(TheData1)):
    print(TheData1(count) )

    print("Array before sorting \n ")
    print("Array After sorting \n")

    Array before sorting

    Traceback (most recent call last):
    File "c:\Users\Admin\Desktop\Paper 4", line 26, in
    File “c:\Users\Admin\Desktop\Paper 4”, line 23, in printarray
    print(TheData1(count) )
    TypeError: ‘list’ object is not callable

    Anyone help how to done slicing in python

  3. pradeep says:

    what does s(5:5) return

  4. Kobinarth Panchalingam says:

    Your idea about reverse a string using a negative value is completely wrong please update this post.
    What actually happening here is

    x = ‘H e l l o w o r l d’ —–0 1 2 3 4 5 6 7 8 9

    >>> x[8:1:-1] output – ‘ l r o w o l l ‘ ——-8 7 6 5 4 3 2

    Usually, the second parameter 1 won’t be taken into consideration right. This means if I enter x[1:8], index 8 won’t be sliced right. Similarly, this happens in x[8:1:-1]. So index 1 won’t be sliced.

    >>> x[-2:-8:-1] output- ‘lrowol’
    Here, -2,-3,-4,-5,-6,-7 indexes will be sliced.

    When p x>y and index y won’t be sliced. That’ it!!!!!!!!

  5. mostafa says:

    please alter your article : the negative indexing is wrong

  6. Coder says:

    For the negative count, the end of the string starts from -1 not 0. It is like -4 -3 -2 -1 not -4 -3 -2 -1 0

  7. Kalpit says:

    I have a doubt:
    string = “Hi There”
    print (string[-4:-2])
    and the output is ‘he’ but shouldn’t the output be ‘eh’?
    I am unable to understand why the output is showing he

    1. Shiv Shakti Tiwari says:

      Since string slicing is started from -4 till -2 and in python slicing is irrespective of indexing , rather it depends upon order of declaration.Things written first are bound to be executed first unless stated otherwise!

    2. Gaurab Dawadi says:

      If you want to print ‘eh’ from ‘Hi There’ you can do

      string = ‘Hi There’

      Here -1 represents reverse order and says go from -3 to -5 but from reverse. This is what I’ve understood, correct me if I’m mistaken.

  8. Enoc says:

    Can you explain the last one a little bit? s[-4:-2] Thanks!

  9. sradhanjali behera says:

    Thankx for this section.
    I need more clarification on

    s1 = s[8:1:-1]
    Output: lroWoll

    I am unable to get it .

    1. Allen Philip Abraham says:

      It is my observation that this article is plagued with an incorrect mental model towards negative indexing.
      The negative index should not start at 0, but it should rather start at a negative length of the list i.e. -(len(s)) .

      1. Rezky says:

        True negative index should start from -1

      2. John Gee says:

        I think you start fromposition 8 and work backwards including position 2. So, you should get lrowoll

      3. Developer 'J' says:

        Negative indexing starts with -1

  10. curry lover 68 says:

    thanks pankaj!!!

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors