A method of converting the specified string to uppercase in Python and getting a new string separated by a space every 2 characters

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.