Python development-case conversion, capitalize first letter, remove special characters

Source: Internet
Author: User

This blog is mainly to summarize our common development process in the string of some operations:

# Letter Case Conversion # first letter to uppercase # remove special characters from string (e.g. ' _ ', '. ', ', ', '; ') and then concatenate the stripped string together # remove ' Hello_for_our_ ' _ ' in world ', and capitalize the first letter of the word from the beginning of ' _       '

The specific code demo:

1#Letter Case Conversion2#Capitalize first letter3#Remove special characters from the string (such as: ' _ ', '. ', ', ', '; '), and then concatenate the stripped string.4#Remove ' _ ' from ' Hello_for_our_world ' and capitalize the first letter of the word from the beginning of ' _ '5 Low_strs =‘Abcd‘6 uper_strs =‘Defg‘7 Test_stra =‘Hello_world‘8 TEST_STRB =‘Goodboy‘9 TEST_STRC =‘Hello_for_our_world‘Ten TEST_STRD =‘Hello__our_world_‘1112#lowercase to uppercaseLow_strs =Low_strs.upper ()14Print‘ABCD Lowercase to uppercase:‘, Low_strs)1516#Uppercase to lowercaseUper_strs =Uper_strs.lower ()18Print‘DEFG uppercase to lowercase:‘, Uper_strs)1920#Capitalize the first letter onlyTEST_STRB = Test_strb[0].upper () + test_strb[1:]22Print‘Goodboy only capitalize the first letter:‘, TEST_STRB)2324#Remove the middle ' _ ', the other symbols are possible, such as: '. ', ', ', '; 'Test_stra ='. Join (Test_stra.split (‘_‘))26Print‘Hello_world remove the middle \ ' _\ ':‘, Test_stra)2728#Remove ' _ ' from ' Hello_for_our_world ' and capitalize the first letter of the word from the beginning of ' _ '29DefGet_str (ORISTR,SPLITSTR):Str_list =Oristr.split (SPLITSTR)31If Len (str_list) > 1:32For indexIn range (1, Len (str_list)):33If STR_LIST[INDEX]! =‘‘:Str_list[index] = Str_list[index][0].upper () + str_list[index][1:]35Else:36Continue37Return‘‘. Join (Str_list)38Else:39 return Oristr40 41 "  remove \ ' _\ ' from ' hello_for_our_world\ ' and capitalize from the first \ ' _\ ' words:  '  ", Get_str (Test_strd, ' _ ')        

Operating effect:

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 32Bit (Intel)] on Win32type"Copyright","Credits"Or"License ()"ForMore information.>>> ================================ RESTART ================================>> >ABCD lowercase to uppercase: ABCDDEFG uppercase to lowercase: defggoodboy only the first letter: Goodboyhello_world Remove the middle‘_ ' : HelloWorld remove " hello_for_our_world_ "and put from the first " _ '  After the first capital of the word: Helloforourworld remove  ' _" and take from the first  ' 

Python development case conversion, capitalize first letter, remove special characters

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.