2.3 Device Operation
2.3.3 Power Losses
The Python Interpreter sequence in Fig.7.5Illustrates some additional operations with strings.SS.endswith(‘sb’) returns True if the string SS ends with the sub-sequence‘sb’; else it returns ‘False’. string S6 in [1] ends with ‘dog’. [2]
confirms this. One has the option of checking for any element in a tuple to be at the end ofSS as illustrated by [3] and [4] with the string S6 itself. Similar checks can be made over a selected segment ofSS also. SS.endswith(‘sb’, p, q) picks out the slice SS[p, q] and checks whether it ends with the sub-string ‘sb’. [5] is an illustration withS6. SS. startswith(‘sb’, p, q) is similar to SS.endswith (‘sb’, p, q); it tests string SS for sb being at the start of the slice SS [p, q] in it.
One can also test for an element in a tuple (used in place of sb) to be at the start of SS [p, q]. [5], [6], and [7] are illustrations of use of .startswith(). A select number of methods is available to check for the presence of different categories of characters in strings.SS.isalpha() returns ‘True’ if the string SS is non-empty
>>> s6 = 'One day there passed by a company of cats a wise
>>> a1, a2 = s6.startswith('One'),s6.startswith('day') [5]
>>> a1, a2 (True, False)
>>> a3 = s6.startswith(('One', 'day')) [6]
>>> a3
>>> for jj in range(len(s6)):
... if 's6[jj]'.isalpha():slt.append(s6[jj]) ...
>>> for jj in ll:lm.append(jj.isnumeric()) ...
>>> lm
[True, False, False, False, False, False, False, False]
Fig. 7.5 Python Interpreter sequence illustrating additional operations with strings
150 7 Operations for Text Processing
and all its characters are alphabetic. Table7.3 lists the different methods of this category with details of the checks they make. Relevant illustrative examples of usage are also shown in the sequence in Fig.7.5.
Strings can be combined and split in different ways. Illustrative examples are in the Python Interpreter sequence in Fig.7.6.SS.split() directly splits SS into a list of all the words in it—words in the sense of groups of characters separated by white spaces. The stringss in [1] is split to form the list sp shown in [2].
Any sequence of strings can be concatenated into one string using‘com’.join (‘sqn’). Here ‘sqn’ is a sequence (like a tuple or a list). The string ‘com’ is interposed between adjacent elements of‘sqn’ in forming the concatenated string.
The string sj in [4] is formed by combining the elements of list sp. A white space—‘‘—is inserted between adjacent elements in it [3]. Sjj in [5] is formed similarly with three white spaces between adjacent elements.Sj and sjj are again split into respective word lists in [6] using the method split() itself. Note that the intervening spaces are ignored whatever be their lengths.Sj0 in [7] is formed by concatenating the elements ofsp with the single character string ‘$’ as the sepa-rator. With split() the argument need not be specified if white space is the separator as was done in [2]. With other separators the separator has to be specified as a string argument.Sj0.split(‘$’) splits string sj0 into its element treating
‘$’ as the separator between adjacent elements [8]. The method .split() has two arguments in its general form—as SS.split(‘aa’, b); here ‘aa’ is the separator string and b the integer signifying thatSS is to be split into b + 1 elements. sp4 in Table 7.3 Methods to test string content: if the specified string is non-empty and the specified test is satisfied ‘True’ is returned; else ‘False’ is returned
isalpha() All are alphabetic characters
isnumeric() All are numeric characters— i.e., digits
isdecimal() All the decimal characters—includes digit characters (0, 1, … 9), and others defined in Unicode
isdigit() All decimal characters and some others like①, ②, ③, … superscript and subscript digits, and so on as defined in unicode
isalnum isalpha() or isdecimal() or isnumeric() isidentifier() Python identifier
islower() All characters of lower case type
isprintable() All are printable characters (excludes control, formatting characters…) isspace() All are white space characters
istitle() The string has to be a title-cased string—first letter in every word is a capital letter
isupper() Every character in the string is in upper case
isalum() Every character in the string satisfies one of SS.isalpha(), SS.
isdecimal(),SS.isdigit(), or SS.isnumeric() startswith
(‘sb’, p, q) SliceSS[p:q] starts with substring sb. One can also use a tuple in place of sb and test whether ss starts with an element in it
endswith(‘sb’, p, q)
SliceSS[p:q] ends with substring sb. One can also use a tuple in place ofsb and test whether SS ends with an element in it
[8] is formed assj0.split(‘$’, 2). Two strings separated by ‘$’ are extracted as
‘Hello’ and ‘how’; the rest of SS is returned as such—as ‘are$you$?’.
Example 7.6 kb below is a quote from ‘The Madman’ by Kahlil Gibran. Redo kb as a string with‘*’ separating the second word from the first, ‘**’ separating the third from the second and so on.
kb = ‘One may not reach the dawn save by the path of the night’.
kbp is the list of words in kb in the same order as in kb itself [12]. n1 in [13] is the total number of words in kb. a1 is initialized to ‘*’ in [14]. The asterisk sequence insertions are to be done len(kb) − 1 times. kb is split into the first word and the rest askbb in [15]; a1 as the single asterisk separator inserted in between in
>>> ss = 'Hello how are you ?' [1]
>>> sp = ss.split() [2]
>>> sp
>>> sp1, sp2 = sj.split(), sjj.split() [6]
>>> sp1, sp2
(['Hello','how','are','you','?'],['Hello', 'how', 'are', 'you', '?'])
>>> sj0 = '$'.join(sp) [7]
>>> sj0
'Hello$how$are$you$?'
>>> sp3, sp4 = sj0.split('$'), sj0.split('$', 2) [8]
>>> sp3, sp4 [9]
(['Hello', 'how', 'are', 'you', '?'], ['Hello', 'how',
'are$you$?']) [10]
>>> kb = 'One may not reach the dawn save by the path of
the night' [11]
>>> kbp = kb.split() [12]
>>> n1 = len(kbp) [13]
>>> a1 = '*' [14]
>>> while n1 > 1:
... kbb = kb.split(None, 1) [15]
Fig. 7.6 Python Interpreter sequence illustrating use of split() method with strings
152 7 Operations for Text Processing
[17] and n1 decremented in [18]. The sequence of operations is repeated until completion.kb in [19] is the desired sequence.
ss.rsplit(‘cc’, d) is the counterpart of ss.split(‘aa’, b) discussed earlier.
ss from Fig.7.6has been reproduced in [1] in the Python Interpreter sequence in Fig.7.7.ss.rsplit() splits ss into words starting from the right end of ss [2] and assigns the resulting set of words tosr in [3]. The words in sr are joined together [4] to form the single stringsj0 in [5]. The ‘&&’ pair is the separator used between adjacent words here.sj0 is split fully into words with sj0.rsplit(‘&&’) [6] removing the separator‘&&’. The splitting is done here starting at the right end. sj0.rsplit (‘&&’, 2) splits sj0 partially—into three parts—stripping away only the last two
‘words’ (‘you and ‘?’) to form the list sr4 [7].
Example 7.7 Repeat the above exercise in Example 7.6 with ‘*’ as separator between the last two elements of kb,‘**’ as the one prior to that separator and so on.
The Interpreter sequence for this is shown from [8] onwards. kb.rsplit (None, 1) is assigned tokbb. In all other respects the sequence is similar to that in Example7.6above.Kb in [9] is the desired output.
The python Interpreter sequence in Fig.7.8a, b illustrate some additional operations with strings.ss.strip() in Fig.7.8a removes all white spaces on either
>>> ss = 'Hello how are you ?' [1]
>>> sr = ss.rsplit() [2]
>>> sr [3]
['Hello', 'how', 'are', 'you', '?']
>>> sj0 = '&&'.join(sr) [4]
>>> sj0 [5]
'Hello&&how&&are&&you&&?'
>>> sr3, sr4 = sj0.rsplit('&&'), sj0.rsplit('&&',2) [6]
>>> sr3, sr4 [7]
(['Hello', 'how', 'are', 'you', '?'], ['Hello&&how&&are', 'you', '?'])
>>> kb = 'One may not reach the dawn save by the path of the night'
>>> n1, a1 = len(kb.split()), '*' [8]
>>> while n1 > 1:
... kbb = kb.rsplit(None, 1) ... kb = a1.join(kbb) ... a1 += '*' ... n1 -= 1
...
>>> kb [9]
'One************may***********not**********reach*********t he********dawn*******save******by*****the****path***of**th e*night'
Fig. 7.7 Python Interpreter sequence illustrating use of variants of split() method with strings
>>> ss = ' One day there passed by a company of cats a
wise dog ' [1]
>>> s6 = ss.strip() [2]
>>> s6
'One day there passed by a company of cats a wise dog'
>>> s6.strip() [3]
'One day there passed by a company of cats a wise dog'
>>> s6.strip('Ognd') [4]
'e day there passed by a company of cats a wise do'
>>> s6.lstrip('e nOd') [5]
'ay there passed by a company of cats a wise dog'
>>> s6.rstrip('a wise dog') [6]
'One day there passed by a company of cat'
>>> s6.replace('c', 'C') [7]
'One day there passed by a Company of Cats a wise dog'
>>> s6.replace('e', 'EE', 2) [8]
'OnEE day thEEre passed by a company of cats a wise dog'
>>> s6.partition('company') [9]
('One day there passed by a ', 'company', ' of cats a wise dog')
>>> s6.partition('Company') [10]
('One day there passed by a company of cats a wise dog', '', '')
>>> s6.find('e ',8, 30) [13]
12
>>> s6.find('e ',15) [14]
47
>>> s6.find('e ',15, 45) [15]
-1
>>> s6[-8:] [16]
'wise dog'
>>> s6.rfind('e ', -8) [17]
47
Fig. 7.8 aPython Interpreter sequence illustrating more operations with strings (continued in Fig.7.8b), b Python Interpreter sequence illustrating more operations with strings (continued from Fig.7.8a)
154 7 Operations for Text Processing
side ofss [1]. The stripped version of ss is s6 in [2]. In the absence of white spaces at the end, withs6.strip() the string remains untouched as in [3]. The generalized version of the strip method isss.strip(‘chr’) where ‘chr’ is a set of characters to be removed. Here all combinations of the specified set in ‘chr’ are removed.
ss6.strip(‘Ognd’) removes ‘O’ and ‘n’ at the left and ‘g’ at the right in [4]. Note that‘o’ at the right end—being the small letter is not removed. Hence
‘d’—precedes ‘o’ and is not at the end—remains untouched. s6.lstrip() and s6.rstrip() are similar to s6.strip(). S6.lstrip(‘e nOd’) removes the ‘e nOd’ combination at left (leading) end [5] and s6.rstrip(‘a wise dog’) does a similar thing at the right end [6]. Again all the respective character combinations are removed in both the cases. ss.replace(‘oo’, ‘nn’, m) replaces the substring
‘oo’ by the substring ‘nn’ in the string ss in the first m occurrences of ‘oo’ (m is an integer here). The integerm is optional. If replacement is sought to be done in the whole of the string‘ss’, m is omitted. s6.replace(‘c’, ‘C’) in [7] replaces all ‘c’ in s6 by ‘C’s. s6.replace(‘e’, ‘EE’, 2) replaces only the first two occurrences of‘e’s in s6 by ‘EE’s [8].
ss.partition(‘prt’) partitions ss into three segments. Thefirst occurrence of
‘prt’ in the string ‘ss’ is identified; ss is split into one substring up to this occur-rence, the substring‘prt’ itself, and the rest of ss as the third subset. As an illus-trations6.partition(‘company’) in [9] splits s6 into three substrings in the line
>>> s6.index('e ',15, 45) [19]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s6.rindex('e ',15, 45) [20]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s6.rindex('e ',15) [21]
47
>> s6t = s6.title() [22]
>>> s6t
'One Day There Passed By A Company Of Cats A Wise Dog'
>>> s6t.swapcase() [23]
'oNE dAY tHERE pASSED bY a cOMPANY oF cATS a wISE dOG'
>>> s6t.lower() [24]
'one day there passed by a company of cats a wise dog'
>>> s6t.upper() [25]
'ONE DAY THERE PASSED BY A COMPANY OF CATS A WISE DOG'
>>> s6.zfill(70) [26]
'000000000000000000One day there passed by a company of cats a wise dog'
(b)
Fig. 7.8 (continued)
following. If the specified substring ‘prt’ is absent in ss, ss along with two empty substrings following it, are returned. [10] searches for‘Company’ (with a capital
‘C’) in s6. It being absent, s6 and the two empty substrings are returned. The method .rpartition() does partitioning similarly; but here the scanning to identify the substring starts at the right end ofss.rpartition(‘EE’) partitions s6 as in [11].
ss.find (‘aa’, ns, ne) identifies the slice ss[ns:ne] in ss, scans it for the presence of the substring‘aa’ in it and returns the index of its first occurrence. If the integerne is omitted the scanning is done in the whole of the substring ss from ss[ns] onwards. If ns is also omitted the whole of ss is scanned to identify the first occurence of‘aa’ and its location. If ‘aa’ is not present in ss ‘−1’ is returned as the index value.S6.find(‘e’) [12] identifies the index of the first occurrence of ‘e’ in s6 as 2. In fact it is the location of the word ending with ‘e’. S6.find(‘e’, 8, 30) [13] returns 12 as the index (corresponding to the word ‘these’). The next occurrence is at index 47 (corresponding to the word‘wise’) as can be seen from [14] where the full sub-sequence froms6 is scanned. S6.find(‘e’, 15, 45) in [15]
confirms the absence of any word ending in ‘e’ in this range by returning the index
−1. ‘e’ in sb scans for the presence of ‘e’ in sb but not for its location. In this respect .find() method is more demanding; the use of the ‘in’ operator suffices when it serves the purpose adequately.
ss.rfind(‘aa’, ns, ne) is the counterpart of ss.find(‘aa’, ns, ne). Here search for the sub-string ‘aa’ is from the right end. Lines following [16 and 17] are illustrations for its use. Functionallyss.index (‘aa’, ns, ne) achieves the same purpose asss.find(‘aa’, ns, ne). In case the sub-string ‘aa’ is not present in ss, a ValueErroris raised. .index() differs from .find() only in this respect. A similar identity holds good forss.rindex() and ss.rfind() as well. s6.index(‘e’, 15) in [18] returns the index value 47. Buts6.index(‘e’, 15, 45) returns ValueError in [19] (Fig.7.8b).s6.rindex(‘e’, 15, 45) in [20] returns a ValueError. S6.
rindex(‘’, 15) returns 47 as the index in [21]. ss.swapcase() swaps the cases of all the letters inss.
The following are additional method with strings—all illustrated through the strings6/s6t.
s6.title() converts the string s6 to title form; the leading letters of all the words in it are turned into capital letters [22].
s6t.swapcase() swaps the cases of all the letters in s6t [23].
S6t.lower() returns a copy of s6t with all the characters converted to lower case [24].
s6t.uper() returns a copy of s6t with all the characters converted to upper case [25].
s6.zfill(70) returns a string of 70 characters as long as len(s6) is 70 or less.
The difference between 70 and len(s6) is made up by filling ‘0’ at the leading (i.e.,. left end) side [26]. If length ofs6 exceeds 70 (or the number in its place) in length,s6 is returned without any change.
156 7 Operations for Text Processing