TechieDrill Your World Of Technical Tutorials

Python Slicing – How to get substring

11.12.2009 · Posted in Python

What we call as substring in C/C++/Java/CSharp/VB is implemented by slicing in python.

Slicing works something like this:

name = ‘techiedrill’

Here, python treats the string as
0-t
1-e
2-c
4-h and so on…

So, to get a part of the original string, all that needs to be done is use the : symbol. This is called slicing.
first = name[:5]           ===>        first = ‘techie’
last = name[5:]            ===>        last = ‘drill’
middle = name[2:6]  ===>        middle = ‘chied’

One Response to “Python Slicing – How to get substring”

  1. manikandan says:

    This is simple inbuilt function !~!!!!

Leave a Reply

You must be logged in to post a comment.