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