Python is one of the easiest programming languages to either newbies or seasoned professionals alike. If you are completely new to 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.
Strings are the most fascinating subject taken any programming language and Java, in specific as I come from that background. But let me tell you that Python has equal number of functions to manipulate Strings and to make 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 about 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:
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 chose 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:
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 backwards.
Python 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:
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 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.
Get Updates on Tech posts, Interview & Certification questions and training schedules