Binary conversion and value types, character string converting related Application

A numerical conversion of the number and type of

  . 1, int string can be converted into digital integer containing pure

  2, binary conversion

  # Turn into other binary decimal

# Print (bin (12 is)) # 0b1100 
# Print (OCT (12 is)) = # 12 is 0o14>. 1 * (. 8 **. 1) 2 + (0. 8 **) 
# Print (hex (12 is)) 0xc # 
# The output 
'' ' 
0b1100 
0o14 
0xc 
' ''

  # Binary conversion (Learn **)
  # hexadecimal to decimal other
    print (int ( '10', 2)) # 2 binary transfer
    print (int ( '10', 8)) # 8 hexadecimal revolutions
    print ( int ( '10', 16) ) # 16 revolutions hexadecimal

  3, the variable type immutable type

    Variable Type: if the value changes, id unchanged, indicating that you change the original value
    # immutable: when the value is changed, id must variant
    # Python variables can be divided into two types
    # immutable type ( Digital , a string, a tuple , not variable set)
    # variable type ( list, dictionaries , set variable)

Second, the use of string conversion

  1, according to the index value (Forward + Reverse take take): can take only (non-modified)

  2, a slice (care regardless of the end, step): the interception of a new small strings from a larger string

    # The left head is fixed, the right place is not fixed according to figures to say the beginning and end points

    Example:

= S ' Hello ' 
Print (S [0:. 5 ])
 Print (S [0: 10: 2])   # step representing every few take one step is not written by default. 1 
Print (S [-1 ]) 
 Print (S [. 5: 0: -1])   # slice is from left to right default values, negative values understand 
Print (S [-1: -1: -10 ])
 # output 
'' ' 
Hello 
HLO 
O 
Olle 
olleh 
'' '

  . 3, len (length):

    Statistics is the number of characters in the string
    S1 = 'Hello'
    Print (len (S1)). 5 #

  4, in and not in (membership operator):

    Determining whether there is a substring of string in a large

  5、strip 

    Removing the string of characters on both sides of the left and right, regardless of the intermediate
    using the built-in method using a unified symbol period (.)
    Strip () Default space removal string end to end,

  . 6, split (splitting)
    for a certain string according to the tissue separator, it can be cut into split list, and further the value for
    # emphasized: split out segmentation data type is a list
    # if not specified, the split and the effect is the same rsplit

data = 'jason|123| handsome'
print(data.split('|'))
username,password,info = data.split('|')
print(username,password,info)
    
#输出结果
#['jason', '123', ' handsome']
#jason 123 handsome

        

  7. .lower () all lowercase string
    All .upper () String uppercase

  8, startswith, endswith: determine what the beginning and ending

  . 9, the format of the three kinds of games are played
    first, by location stations (% s principle and has been)
    Example:. Str1 = 'my name is {} my age is {}' format (18, 'jason',)
    a second species, according to the index placeholder
    Example:. str1 = 'my {1 } name is {0} my {0} age is {0}' format ( 'egon', 18)
    a third, by name Road name placeholder ( keyword-parameters)
    Example:. str1 = 'my {name } name is {age} my {name} age is {name}' format (name = 'jason', age = 18)  

  10, join: the plurality of types of splice elements of the container into a specified character string from
    Example:

= RES [ ' Jason ' , ' 123 ' , ' Handsome ' ] 
STR = ' $ ' .join (RES)   
 Print (STR)
 # output 
# Jason $ 123 $ Handsome

 

  11, replace the replacement characters in the string (optional number)
    Example:

str = 'egon is dsb and egon is sb he has a BENZ'
res = str.replace('egon','kevin',1) 
#输出结果
# kevin is dsb and egon is sb he has a BENZ

  12, isdigit # determines whether a string contains purely digital

  13、find、rfind、index、rindex、count

= S ' Kevin Kevin IS and O IS DSB SB ' 
Print (s.find ( ' DSB ' ))    # returns the index where the d-characters 
Print (s.find ( ' XXX ' ))    # can not find the time no error returns -1 
Print (s.find ( ' I ' , 0,3))    # can also be limited by the look index 
Print (s.index ( ' O ' ))    # index character where the return pass value 
Print (s.index ( ' i ' , 0,5))    # returns the index where the preaching character, if no will complain 
Print (s.count (' N- ' ))    # number of characters that appear in statistic 
# output 
'' ' 
. 9 
-1 
-1 
13 is 
. 3 
. 3 
' ''

  14, center (middle), ljust, rjust, zfill (supplemented with 0) Usage

= s9 ' tom ' 
print (s9.center (12, ' * ' ))
 print (s9.ljust (6, ' $ ' ))
 print (s9.rjust (6, ' $ ' ))
 print (s9.rjust ( 6, '  ' ))
 print (s9.zfill (6 ))
 # 输出结果
'' ' 
**** ***** tom 
tom $$$ 
$$$ tom 
   tom 
000tom 
' ''

  16, expandtabs few more Tab key

= S10 ' A \ TBC ' 
Print (s10.expandtabs (10 ))
 # output 
# A BC

 

  15, captalize (first letter capitalized), swapcase (invert case), title (the first letter of each word capitalized)

= S12 ' HELLO WORLD SH10 ' 
Print (s12.capitalize ())   # the Hello world capitalized 
Print (s12.swapcase ())   # invert case 
Print (s12.title ())   # the first letter of each word in the size 
# output 
'' ' 
the Hello World SH10 
HELLO WORLD SH10 
the Hello World SH10 
' ''

 

  16, is a digital series
      '' .isnumeric (): unicode, Chinese figures, insofar as it represents the Roman numeral numbers are identified.
      '' .isdecimal (): unicode recognizes only ordinary Arabic numerals.
      '' .isdigit (): bytes, using the unicode isdigit usually has to meet the needs of

 

Guess you like

Origin www.cnblogs.com/xiaowangba9494/p/11128279.html