In the example, for the specified string:
Faa5fbb5fcc5fdd5010200000028000001900000000a002d00000000017d7840000003e800005fa55fb55fc55fd5
Three methods were used to convert them to uppercase letters and each 2 characters were separated with a space to get a new string, as follows:
FA A5 FB B5 FC C5 FD D5 E8 xx (xx) 0A xx 2D xx xx xx (7D)----5F A5 5F B5 5F C5 5F D5
In order to verify the efficiency of three methods, the original string is expanded to 100 times times the length, and then 10,000 processing, from its time-consuming results, the first method is the slowest, the second to third method is better.
Code:
1 #-*-coding:utf-8-*-#2 3 #-------------------------------------------------------------------------------4 #Name:mysplit5 #Description: Converts the specified string to uppercase and splits every 2 characters with a space to get a new string6 #Author:administrator7 #DATE:2018/7/68 #-------------------------------------------------------------------------------9 Ten ImportRe One Import Time A - #string to be split -MyStr ='Faa5fbb5fcc5fdd5010200000028000001900000000a002d00000000017d7840000003e800005fa55fb55fc55fd5' the #after split: FA A5 FB B5 FC C5 FD D5-E8 (XX) (0A) 2D (xx) xx (7D) 00 00 5F A5 5F B5 5F C5 5F D5 - - defmySplit1 (str): -t="' + forIinchRange (len (str)/2): -T + = str[2*i:2* (i+1)] +' ' +t =T.upper () A returnT at - defmySplit2 (str): -t =Str.upper () -p = re.compile ('. {A}')#match any character 1-2 times - return ' '. Join (P.findall (t)) - in defmySplit3 (str): -t =Str.upper () to return ' '. Join ([t[2*i:2* (i+1)] forIinchRange (len (t)/2)]) + - Print('Original string: \ n'+ MyStr +'\ n') the Print('post-converted string:') * Print('mySplit1:'+mySplit1 (mystr)) $ Print('MySplit2:'+mySplit2 (mystr))Panax Notoginseng Print('mySplit3:'+mySplit3 (mystr)) - the Print(U'\ nthe time-consuming test:') +mystr = mystr * 100 A forFinch[MySplit1, MySplit2, mySplit3]: thet =time.time () + forIinchRange (10000): - f (mystr) $ Print(F.func_name +': '+ str (time.time ()-T) +'s')
The results of the operation are as follows:
A method of converting the specified string to uppercase in Python and getting a new string separated by a space every 2 characters