Python Basic Skills Comprehensive training question 1

Source: Internet
Author: User

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

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.