Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Binary conversion and string conversion in Python

Shulou Source: shulou.com Published: 2022-06-02 05:22:23 09月17日 Update

This article introduces the knowledge of "binary conversion and string conversion in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One ten turns two

Convert decimal to binary:

> > bin (10)

'0b1010'

2 ten to eight

Convert decimal to octal:

> oct (9)

'0o11'

Three ten to sixteen

Convert decimal to hexadecimal:

Hex (15)

'0xf'

4 string to byte

Convert string to byte type

> s = "apple"

> bytes (srecoery encodingbirds utfmur8')

Baked apple`

5 converted to a string

Convert character types, numeric types, etc., to string types

> I = 100

> str (I)

'100'

6 + 10 rpm ASCII

ASCII characters corresponding to decimal integers

Chr (65)

'A'

7 ASCII to 10

Decimal number corresponding to ASCII character

> ord ('A')

sixty-five

8 convert to a dictionary

Several methods of creating data dictionary

> > dict ()

{}

> dict (axioms, girls, books, books, etc.)

{'await:' await, 'baked:' b'}

> dict (zip (['axiaojingzhongb'], [1pd2]))

{'averse: 1,' baked: 2}

> dict ([('axiaojin1), (' baked page2)]))

{'averse: 1,' baked: 2}

9 convert to floating point type

Convert an integer or numeric string to a floating point number

> > float (3)

3.0

If it cannot be converted to a floating point number, it will report ValueError:

> float ('a')

Traceback (most recent call last):

File "", line 1, in

Float ('a')

ValueError: could not convert string to float:'a'

10 convert to integer

Int (x, base = 10)

X may be a string or numeric value, converting x to an integer.

If the parameter is a string, it may contain symbols and decimal points.

If the integer representation is outside the range, a long integer is returned.

> int

eighteen

11 turn into a collection

Returns a set object. Duplicate elements are not allowed in the collection:

> a = [1, 4, 2, 3, 1]

> > set (a)

{1, 2, 3, 4}

12 turn to slice

Class slice (start, stop [, step])

Returns a slice object with an index set specified by range (start, stop, step), making the code more readable.

> a = [1, 4, 2, 3, 1]

> my_slice = slice (0meme 5pm 2)

> > a [my _ slice]

[1, 2, 1] the content of "binary conversion and string conversion in Python" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Characters strings decimal integers decimals types numeric values decimal content dictionaries bytes objects more points knowledge utility learning and then binary code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Shulou Information Huawei Redmi