Different syntax and transformations in Python

Source: Internet
Author: User

Different ways of writing in the system
    • Octal (octal) 0o377
    • Hex (hex) 0xFF
    • Binary binary (binary) 0b11111111
Conversions between different binaries

Python provides three built-in functions that can be used to convert between different binaries.

>>> oct(255), hex(255), bin(255)(‘0o377‘, ‘0xff‘, ‘0b11111111‘)

You can also use the int function to turn a string into a numeric value

>>> int(‘255‘), int(‘0xFF‘, 16)(255, 255)

In addition to the ability to use eval, the function is similar to the int function, but its parameters are Python code.

>>> eval(‘255‘), eval(‘0xFF‘)(255, 255)

Of course, you can also use the formatted output of a string

>>> ‘{0:0}, {1:x}, {2:b}‘.format(255, 255, 255)‘255, ff, 11111111‘>>> ‘%o, %x, %X‘ % (255, 255, 255)‘377, ff, FF‘

Different syntax and transformations in Python

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.