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)
mystr = "this is a string" print(mystr.split()) mystr1 = "this is also a string" print(mystr1.split())
['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'))
['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))
strip() method :
['th', 's ', 's a string'] ['th', 's ', 's also a string']
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))
txt = " ,,,,,dkdk.....cat....dkdkp" print(txt.strip(" ,.dkp")) print(txt.strip(" ,.dkt"))
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())
find() and index() method:
PY4U IS AN AMAZING SITE py4u is an amazing site
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)
- 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))
count() method :
3 3 3
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'))