1, Case flip
>>> str='hello,ghostwu'>>> str.swapcase ()' HELLO,GHOSTWU'
2, extracting a combination of pure numbers from a string of strings
>>> str="adfask22jkljhh3jkljhgh435""inif S.isdigit ()])'223435'
Equivalent to:
>>> str="adfask22jkljhh3jkljhgh435">>> L = []>>> forSinchstr: ...ifs.isdigit (): ... l.append (s) ...>>>l['2','2','3','4','3','5']>>>"'. Join (L)'223435'>>>
3, count the number of occurrences of the character, with the character as the key, case as the same character
>>> s ="Abcsabcdefabcabcdfea">>> s =S.lower ()>>>s'Abcsabcdefabcabcdfea'>>> res = Dict ([(Key, S.count (key)) forKeyinchset (s)])>>>res{'a': 5,'C': 4,'b': 4,'e': 2,'D': 2,'F': 2,'s': 1}>>>
4, String de-weight, output in the original order
>>>s'ABCCCABDEFDX'>>> L =list (s)>>> set_list =list (set (L))>>> res = Set_list.sort (key=L.index)>>>Res>>>set_list['a','b','C','D','e','F','x']>>>
5, String inversion
' ABC '>>> s[::-1]'CBA'
6, remove the numbers in the string, and then sort, if the same letters appear, such as AA, uppercase letters in front of lowercase letters
#!/usr/bin/python#Coding:utf-8s='ABCDBA233ABC1'L=Sorted (s) upper_list=[]lower_list= [] forVinchL:ifv.isupper (): Upper_list.append (v)elifv.islower (): Lower_list.append (v)Else: Pass#print upper_list, lower_list forXinchUpper_list:x_lower=X.lower ()ifX_lowerinchLower_list:lower_list.insert (Lower_list.index (X_lower), x)Print "'. Join (Lower_list)
Python Basic Skills Comprehensive training question 1