Python Substring

  • (5.0)
  • | 1398 Ratings |
  • Last Updated September 14, 2017

Python Substring

General Introduction to Python Programming language:

Python is one of the easiest programming languages to either newbies or seasoned professionals alike. If you are completely new to a programming language, you should be able to pick up the concepts at a quicker pace and if you already have some coding knowledge (whatever might be the programming language that you have already learnt), learning Python is just a piece of cake.

If you’re here then we assume that you are very much interested in getting your hands dirty with the Python programming skills, so why delay? Let us deep dive into the basics of the programming language. Considering that you have enough knowledge on the programming language itself and a little coding skills, let’s check all the possible string functions made available for the Python developers.

Interested in mastering Python Course? Enroll now for a FREE demo on Python Training.

Python sub-string functions:

Strings are the most fascinating subject taken in any programming language and Java, in specific as I come from that background. But let me tell you that Python has an equal number of functions to manipulate Strings and to make a life of Python developers easy. Now with no more further delay, we will try to check how we will be able to get a substring out of a string in Python programming language.

Python has a handy feature called SLICING that applies to arrays as such, but Strings are no different from them in Python. Strings can be represented as arrays of characters and hence the same feature of slicing applies on to Strings as well. If you remember that we already have discussed arrays and how slicing can be achieved on an array earlier. Let us now take an example and see how that can be applied on strings as well:

Python Substring

In the example above, we have seen how a string can be manipulated to strip the needed part of it using the SLICING technique as applicable to arrays in Python programming language.

  • You can get an individual character of the string by referring to its index relative to the string.

  • You can leave the beginning unattended if you want to get a substring starting from the very first letter to a certain index that you provide

  • You can leave the ending without giving any value if you want to get a substring from the index as provided until the end of the string.

  • You can even choose to not to provide a begin or an end index, in which case the whole string gets returned as the substring without stripping off any characters out of the main string.

Related Article: Code Introspection In Python

What if you wanted to do the same, but in the reverse order? Let us extend the same example to see how this can be achieved and not to worry, explanations always follows:

Python reverse order

The only difference that you would have observed from the previous example to this is just an additional colon being used in the commands above. If you have observed the difference, congratulations, you are having a keen eye on the syntax which is a good sign that you are progressing well on learning the Python programming language. The extra colon in the syntax above, tells Python that it is an extended slice and the index -1 is used to traverse the string backward.

Related Article: Serialization In Python

Alongside slicing, you could use the split() function to derive substrings out of a given string using the Python utility method. The split() functionality expects that you provide a delimiter based on which the provided string is split into substrings and if one is not provided then by default the delimiter is taken as a space character. Let us now take an example of this and see how this can be achieved using the split() function provided by Python programming language:

Python split function

In this article, we have understood how the string manipulation can be done using the SLICING technique and also using the split() function provided by the Python programming language itself. Though there is no standard way of doing it using Python, both are preferred as a way of splitting strings but the usage as always depends on the scenario. Hope the concept of substrings is clear, please provide us comments if you find anything needs to be refined here on this article.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!


Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Free Demo Popup -->