Conversion of Python Data Types

Source: Internet
Author: User

Conversion of Python Data Types

When we are processing data, some data types are not what we want. What should we do?

1. Data type conversion: type to be converted (data)

  • Convert num01 to an integer: int (num01)
  • To convert num01 to a floating point: float (num01)
  • To convert num01 to a string: str (num01)
  • Convert nun01 to boolean: bool (num01)

 

For example:

Print (int ("12345") + 1)

Print (float ("12.345") + 1.187)

Print (str (123) +"456")

Print (bool (1 ))#As long as it is not converted from zeroTrue, 0Is False

Print (int (123.456 ))#Convert to a value after the decimal point

Print (int ("Sandy"))#Cannot be converted, not a basic number requirement

Note: During data type conversion, not all conversions can be successful and exception handling is required.

 

2. Convert numeric values into characters

For example:

Print (ord ("X"))

Print (chr (88 ))

 

Iii. hexadecimal conversion

For example:

Print (hex (200 ))

Print (oct (0, 200 ))

Print (bin (200 ))

 

Demo:

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.