Python formatted format and binary conversion

Source: Internet
Author: User

Format ()

Positional parameter {}

>>> "{0} love {1}.com". Format ("I", "cat")

' I Love cat.com '


Keyword parameters

>>> ' {I} love {cat}.com '. Format (i= "You", cat= "Me")

' You love me.com '


Combining positional and keyword parameters, the positional parameter must precede the keyword argument

>>> ' {0} love {cat}.com '. Format ("You", cat= "Me")

' You love me.com '


%c formatted character and its Ascⅱ code

%s formatted string

%d formatted integer

%o formatting unsigned octal numbers

%x Formatting unsigned hexadecimal

%x formatting unsigned hexadecimal (uppercase)

%f Formatted fixed-point number to specify the precision after the decimal point

%e to format fixed-point numbers with scientific notation

%E function with%E

%g depends on the size of the value to use%f or%e

%G function with%G


Cases

'%d +%d =%d '% (4,3,7+8)

→4 + 3 = 15


Auxiliary operator directives

M.N m is the smallest total width displayed, and n is the number of digits after the decimal point

-For left alignment

+ Show plus sign (+) in front of positive number

# 0 (' 0o ') is displayed in front of octal number, ' 0x ' or ' 0X ' before hexadecimal number

0 display number preceded by ' 0 ' to replace space


>>> '%5.1f '% 27.658 total width 5, 1 digits after decimal point

→ ' 27.6 '


>>> '%10d '% 5

→ ' 5 '


>>> '%-10d '% 5

' 5 '


>>> '%+d '% 5

' +5


>>> '% #o '% 10

' 0o12 '


Decimal Turn binary

Remainder

2/150

2/75 0 150/2 Business is 75, more than 0

2/37 1 75/2 Business is 37, more than 1

2/18 1 37/2 Business is 18, more than 1

2/9 0 18/2 Business is 9, more than 0

2/4 1 9/2 Quotient is 4. More than 1

2/2 0 4/2 Business is 2, more than 0

1/2 0 2/2 Business is 1, more than 1

0 1 2/2 Business is 1, more than 1

A binary number of 150 is 10010110.


Binary goto Decimal

1 0 0 1 0 1 1 0

1*2**7 0*2**6 0*2**5 1*2**4 0*2**3 1*2**2 1*2**1 0*2**0

150


Binary turn octal

Binary octal

000 0

001 1

010 2

011 3

100 4

101 5

110 6

111 7


010100.011101

Conversion from right to left, less than 0. Each three-bit represents a eight-bit

24.35


8 and 9 are not in the octal system


Hexadecimal

0~9 a~f


hexadecimal and binary correspondence relationship

0 1 2) 3 4

0000 0001 0010) 0011 0100

5 6 7) 8 9

0101 0110 0111) 1000 1001

A B C D E F

1010 1011 1100 1101 1110 1111


Binary to hexadecimal method: take four in unity

From left to right, 0 when insufficient


Hex Turn Binary

/2, 0 when insufficient


Decimal to octal and hexadecimal

/8

/16

Remainder

8/150

8/18 6

8/2 2

8 2

226


16/150

16/9 6

0 9

96


octal decimal and hexadecimal

150 = 226 = 2*8**2 + 2*8**1 + 6*8**0


Hexadecimal goto Decimal

150 = 96 = 9*16**1 + 6*16**0


String escape character meaning

\ ' single quotation mark

\ ' double quotes

\a emits system ring tones

\b backspace

\ n line break

\ t horizontal tab (tab)

\v Portrait Tab

\ r return character

\f page Break

\o the characters represented by the octal number

\x hexadecimal number represents the character

\ s represents a null character

\ \ Counter slash










Python formatted format and binary conversion

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.