Python character type

Source: Internet
Author: User

Number type: plural: x=1+2j #1为实数, 2j is imaginary print (x.real) print (X.IMAG) Variable: value change, id invariant, variable = = Non-hash immutable: value changed, ID changed, immutable = = can hashprint (hash (123)) numeric types and string types are immutable string types: A string of characters defined within the "," "," ". mag= ' Hello ' 1. By index value (positive fetch + reverse fetch): can only take print= (Mag[0]) print= (Mag[-1]) 2. Slice (Gu Tou regardless of tail, step) also applies To list print (Mag[0:4]) #0为首, 4 for tail Hellprint (Mag[1:4:2]) #1为首, 4 for the tail, 2 for the step, 2 to take once. Lprint (mag[:]) #开始取到结尾rint (mag[-1::-1]) #倒着取出所有值3. Length: Lenprint (len mag) equals print (mag.__len__ ()) 4. Member operations in and not Inprint (' Llo ' in mag) print (' Llo ' not in Mag) 5. Remove the blank strip: Clear only the empty characters of 2 sides password= ' 123456 ' Pritn (Password.strip ()) Password=input (' >&gt: '). Strip () Lstrip (go left) Rstrip (go right) 6. Slicing splituser= ' Root:x:0:0::/root:/bin/bash ' res=user.split (': ') # The default is space-delimited print (res[0]) rootpath= ' c:\\a\\d.txt ' Print (path.split (' \ \ ', 1)) #1为只分隔一次print (path.rsplit (' \ \ ', 1)) # Cut from right to left 7. Loop mag= ' hel ' N=0size=len (MAG) while n < Size:print (mag[n]) n+=1for i in Mag:print (i) for I in Range (0,5,2): Print ( i) 8. Case Change print (' Ajisdf '. Lower ()) #变小写print (' AsdF '. Upper ()) #变大写9. StartsWith (starting with what), EndsWith (ending with what) MA=AMK Thuiprint (Ma.startswith (' a ')) pRint (Ma.endswith (' I ')) 10.format value print (' My name is%s '%s '% (' lx ', min)) ' Print (' My name Is () ' "Age is () ' Format (' LX ', 78 ) print (' My name is ' (x) ' (y) ' Format (y=78,x= ' lx ') ' Print (' My name is (0) ' (1) ' (0) ' Format (' LX ', 78) ' 11. Join (and split opposite) only used for string type list stitching inct= ' Root:x:0:0:/root:/bin/bash ' l=inct.split (': ') print (': '. Join (L)) 12.replscemag= ' My is Koko my are toto my is Bubu ' mag=mag.replsce (' my ', ' Kawa ', 1) #默认替换所有, 1 for one time print (MAG) 13.isdigit to determine if the value is a number type password= ' 123 ' If Password.isdigit (): password=int (password) 14.find,rfind (from right), Index,rindex (lookup), count (statistics) mag= ' My is ksis ' Print (Mag.find (' is ')) print (Mag.index (' was ')) print (Mag.count (' is ')) 15.center (padding), Ljust (left-aligned), Rjust (right-aligned), Zfill ( Fill with 0) print (' Hi ', center (+, ' # ')) #30个字符, not enough to fill with #. Print (' Hi ', ljust (+, ' # ')) print (' Hi ', Rjust (' # ')) print (' Hi ', Zfill (' # ')) 15.expandtabsprint (' Hello\tword '). Cxpandtabs () 16.captalize (capitalized), swapcase (case reversal), title (capital of each word) print (' I is Kooll '. Catpalize ()) 17. Is Digital Judge Num1=b ' 2 ' #bytesnum2 =u ' 4 ' #unicode, Python3 no need to add UNUM3= ' two ' #中文数字num4 = ' IV ' #罗马数字isdigit, (for Bytes,unicode) print (Num1.isdigit ()) print (Num2.isdigit ()) Print (Num3.isdigit ()) Print (Num4.isdigit ()) Isdecimal, (for Unicode) print (Num2.isdigit ()) print (Num3.isdigit ()) print (Num4.isdigit ()) IsNumeric, (for Unicode, Chinese, Roman) print (Num2.isdigit ()) print (Num3.isdigit ()) print (Num4.isdigit ()) name= ' mis123 ' Print ( Name.isalnum ()) #字符串由字母和数字组成print (Name.isalpho ()) #字符串只由字母组成print (' print '. Isidentifier ()) #判断关键字print (' Abd '). Islower ()) #判断全是否为小写print (' ASD ' Isupper ()) #判断大写print (". Isspace ()) #判断空格print (' Adj '. Istitle ()) #判断标题, That is, the first letter of the word is capitalized 17. List: 1. Value by index (positive fetch + reverse fetch): only 2. Slice (Gu Tou regardless of tail, step) 3. Length: Len4. Loop 5. Member operations in and not in ... mibs=[' cc ', ' AA ', ' RR ']print (list[' Kasidhui ')) #把字符, reload list to add after list: Mibs.append (6) Delete: Del mibs[2]mibs.remove (' CC ') Delete and remove results: Resd=mibs.pop (2) #默认从末尾删print (Reds) loop list: For I in Mibs:print (i) Insert within list: mibs=[' cc ', ' AA ', ' RR ']mibs.insert (2, ' BB ') #2为位置数, BB for value print (MIBs) ' cc ', ' AA ', ' BB ', ' RR ' empty list value: Print (Mibs.claer ()) Copy a list: Mm=mibs.copy () Number of statistics list values: print ( Mm.count (' AA ')) add multiple values to the list: hh=[' RT ', ' Ty ']print (mm.exTend (HH)) returns the position of the value of the list: mibs=[' cc ', ' AA ', ' RR ']print (mibs.index (' AA ')) reverses the value of the list: mibs=[' cc ', ' AA ', ' RR ']print (mibs.reverse ()) [' RR ', ' AA ', ' cc '] sort the list values: Hy=[11,2,8,15]hy.sort () print (HY) [2,8,11,15]print (Hy.sort (reverse=true)) [15,11,8,2]x = ' Ahoe ' y= ' V ' x<y, to determine the first letter of the sort, a-Z from small to large, the number is greater than the letter 18. Tuples: Multiple values are stored, the tuple is immutable (which can be the key of the dictionary) for the list, mainly for read definition: Compare to List type, just [] to () 1. Value by index (positive fetch + reverse fetch): only 2. Slice (Gu Tou regardless of tail, step) 3. Length: Len4. Loop 5. Member operations in and not in ... Age= (11,22,33,44) equals Age=tuple (11,22,33,44) 19. Dictionary: Store multiple values, Key:value access value fast, unordered definition: Key must be immutable type (int,float,str,tuple), Value can be any value 1. By index (positive fetch + reverse fetch): can only fetch 2. Slice (Gu Tou regardless of tail, step) 3. Length: Len4. Loop ... info={' name ': ' Dachui ', ' Age ': ' ' sex ': ' xxx '} equals info= Dict (age=18,sex= ' xxx ', name= ' Dachui ') info=dict ([' Name ', ' Dachui '), (' Age ', ' + '), (' Sex ', ' xxx ')]) info=dict ([[' Name ', ' Dachui '],[' age ', 18],[' sex ', ' xxx ']) Create an empty Dictionary: Info={}.fromkeys ([' Name ', ' age ', ' sex '],none) #value为None删除: info={' Name ': ' Dachui ', ' age ':, ' sex ': ' xxx '}print (info.pop (' name ')) #没有默认值print (Info.popitem (' name ': ' Dachui ')) key, value , key value to items () info={' name ': ' Dachui ', ' age ': ' Sex ': ' xxx '}PRInt (Info.keys ()) #结果不是一个列表print (list (Info.keys ())) #结果为一个列表print (Info.values ()) #结果不是一个列表print (list (Info.values () )) #结果为一个列表print (Info.items ()) #结果不是一个列表print (list (Info.items ())) #结果为一个列表案列: Shopping cart mas={' clothes ': ' 200$ ', ' pants ': ' 2000$ ', ' car ' : ' 55555$ ', ' Hammer ': ' 45454$ '}goods=[]while true:for K in Mas:print (K,mas[k]) chice=input (' Product Name: '). Strip () print (chice) If Len (chice) = = 0 or Chice not in Mas:print (' Product name does not exist ') continue while true:num=input (' Purchase number: '). Strip () if Num.isdigit (): Break Goods.append (Chice,mag[chice],int (num))) print (' Shopping cart ', goods) break20. Set: function: Relational operation, de-redefinition: comma-delimited within {} Each element must be a immutable type. Unordered 1. By index (positive fetch + reverse fetch): can only fetch 2. Slice (Gu Tou regardless of tail, step) 3. Length: Len4. Loop 5. Member operations in and not in ... l={' SDF ', ' dd ', ' GG '}k={' dd ', ' gr ', ' EER '} intersection:& all existing values print (L & k) Collection: | All values, go to heavy result print (L | k) Cross complement: ^ value other than intersection print (l ^ k) difference set:-Two set subtraction print (L-K) parent set, subset: >,>=, <,<= parent set contains subset s1={1,2,3,4} # Parent Set s2={1,2,3} #子集print (S1 > S2) #Ture列表转换为集合: h=[' A ', ' B ', ' C ', ' A ', ' a ']print (seat (h)) difference_update () Subtract and update: s1={ 1,2,3,4} s2={1,2,3} print (Difference_update (s2)) disCARD Specifies the value of the Delete collection remove the element that is not in the error isdisjoint when there is no common part of the two collection, the value is added for true add,update .... Category: Occupy Space: Collection < tuples < List < dictionary number of stored values: only one value (number, string) can be saved, multiple values (list, tuple, dictionary) mutable: list, dictionary, set immutable: numeric, string, tuple direct access: Numerically ordered (ordinal class type): String, List, Tuple key value access (mapping type): Dictionary unordered: Set, dictionary character encoding 1. The character according to what standard to be decoded according to what standard, here the standard refers to the character encoding 2.unicode--->encode--->gbk gbk----> The character encoding used by the decode---->uicode Read 3.python3 interpreter is the ASCII-specified interpreter used by the UTF-8 Python2 interpreter by default: #coding: Gbk4.pythone2 str is python3 bytes python2 Unicode is Python3 str

Python character type

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.