String: A helping hand in python by Aditya Verma

Ever seen yourself all tangled up when you are dealing with threads of cotton called strings? It makes your job tedious and makes your life miserable, right? Well, guess what, Guido van Rossum decided to change your perspective about strings by introducing several operations on strings which were in the ‘wish list’ of all developers. But what is a string? Do you need to carry one while coding in python? The answer is a big NO.

In python, a string is a sequence of characters. The number of operations that can be performed on it… well, that depends upon how many you can remember.

Before diving into the pool of string functions let’s talk about the property of strings in python.

Properties of strings

1.      Strings do no support character type.

2.      They are immutable. The advantage of this property is that when various variables are pointing to same object if one changes then others remain same.

3.      They are ordered block of text.

4.      Strings are concatenated with the ‘+’ sign.

 5.      Strings are repeated with ‘*’ sign.

  6.      Python indexing starts from 0 and goes till (length of string)-1. 

7.      In python, the strings have reverse indexing as well which means the last element of string will have an index of [-1].

Basic operations on string:

  1. Str[i] - Outputs the ith index element.

  2. len(Str)- Returns the length of the string.

  3. Str[:i]- Returns elements from starting to (i-1)th index

  4. Str[i:]- Returns elements from ith index till end.

  5. Str[::k]- Returns elements with an increment of k characters.

  6. Str[::-1]- Returns elements in reverse order.

Commonly used string methods are:

Consider a string with name ‘Str’ and its initial value is “i love python”. Remember that a method only returns the value. They do not change the value.

1.      Str.capatilize()- Converts the first character to upper case.

2.      Str.casefold()- Converts string into lower case.

3. Return a centered string.

4.      Str.count()-Return the number of times a specified value occurs in the string.

5.      Str.startswith()- Returns True if the string start with specified value.

6.      Str.endswith()- Returns true if the string ends with specified value.

7.      Encoding and decodinghas never been so easy. Just use the encode() method with the format in which you want to convert your string and voila. You get the string in encoded form. But now you want your original string back. No worries. Python got you covered. Just use the decode() method to get it back.

These were just a few functions that help us in getting the job done in a one-liner. Remember the time when you had to write 15-20 lines of code to check if a string is a palindrome. In python, you can do that in just 3-4.

It can't get any better than that. Try out some of these functions on your own and be amazed. I hope you learned something new. See you next week.


Contact Us:

A-116, The Corenthum A-65,

Sector 62

Noida 201301

UP, India

Phone: +91 8882050481


© 2020 TheIkigaiLab  Terms of Use Privacy and Security Statement