Methods for Strings

Python has a large number of built-in methods when it comes to dealing with strings. Here i will be mentioning some very frequently used methods.

spilt() method :

split() method in Python is used to return a list containing sub-strings of the original string after a separator . The default separator is any whitespace however specific separators can be passed within the parenthesis. It has the following syntax : string.split(separator, maxsplit)

  • Separator(optional) : Specifies the separator to use when splitting the string
  • maxsplit(optional) : Specifies the number of sub-strings in a list. The number of sub-string in a list will be (maxsplit + 1)

Note that often times you’ll find yourself using the split() method to remove whitespaces within a string. split() method is highly useful when you want to take multiple space-separated strings and you want to store them in a list without any whitespaces( for better indexing). Let us go ahead and run a few codes based on this method.
mystr = "this is a string"  print(mystr.split())  mystr1 = "this is also a string"  print(mystr1.split())

output :
['this', 'is', 'a', 'string']  ['this', 'is', 'also', 'a', 'string'] 

In the above example , the separator is any whitespace so whenever Python sees a whitespace it stores the string before it as an object in the list. In the example below everything is same as above but instead of whitespace, ‘i’ is the separator here.

mystr = "this is a string"  print(mystr.split('i'))  mystr1 = "this is also a string"  print(mystr1.split('i'))

output :
['th', 's ', 's a str', 'ng']  ['th', 's ', 's also a str', 'ng']

In extension to the above example , let us specify the maxsplit value to 2 meaning the list will contain 3 objects.Note that when maxsplit value is set to 2 , successful spliting will be performed only twice in the string and the third object in list will simply contain the remaining string.

mystr = "this is a string"  print(mystr.split('i',2))  mystr1 = "this is also a string"  print(mystr1.split('i',2))

output :
['th', 's ', 's a string']  ['th', 's ', 's also a string']

strip() method :

The strip() method is used to return the trimmed version of a string . The characters you wish to remove from the string should be included within the parenthesis. Note that the default character is a whitespace. It has the following syntax : string.strip(character(s))

The strip() method works from both sides of a string. It goes onto to remove the characters specified in the set untill it finds an character that is not specified in the set and it stops there. I have explained this in the code given below.
txt = " ,,,,,dkdk.....cat....dkdkp"  print(txt.strip(" ,.dkp"))  print(txt.strip(" ,.dkt"))

output :
cat  cat....dkdkp 

Notice how the first strip() method results in ‘cat’ while second does not. This is because the character ‘p’ is not specified in set , meaning that there wont be any stripping from the right side of the string. From the left side of the string , the stripping is carried on till character ‘c’ and ends there.

upper() and lower() method :

upper() and lower() methods are used to convert a string into upper case and lower case respectively. Both of these methods do not have any parameters.Let us take an example to see them in action.

mystr = "PY4U is an amazing site"  print(mystr.upper())  print(mystr.lower())

output :
PY4U IS AN AMAZING SITE  py4u is an amazing site

find() and index() method:

The find() and index() method are almost similar. Both of them look for a specified value and returns its index.The only difference between the two is when the specified value is not found in a string than find() returns -1 while index() raises an exception. They have the following syntax :

  • string.index(value, start, end)
  • string.find(value, start, end)

Parameters:

  • value(required) : The value to search for in a string.
  • start(optional) : The starting point of the search . The default value is 0.
  • end(optional) : The ending point of the search . The default value is the end of the string .

Let us understand these methods with help of an example .

mystr = "hi my name is wadu"  print(mystr.index('m'))  print(mystr.find('m'))  #let us specify the starting and ending points  print(mystr.find('m',0,5))

output :
3  3  3

count() method :

The count() method in Python returns the number of occurrence of a specified value in a string. Note that it is essential that a value to be searched is specified within the parenthesis. Let us take an example to understand this method.

mystr = "hello world"  print(mystr.count('l'))

output :
3