Python data type conversion

Usually when we process data, some data types are not what we want, what should we do?

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

· To convert num01 to an integer: int(num01)

· To convert num01 to a floating point number: float(num01)

· To convert num01 to a string: str(num01)

· To convert nun01 to boolean type: bool(num01)

 

E.g:

print(int("12345")+1)

print(float("12.345")+1.187)

print(str(123)+"456")

print ( bool ( 1 )) #As long as it is not zero converted, it is True, and 0 is False

print ( int ( 123.456 )) # Convert to the value after the decimal point is removed

print ( int ( "Sandy" )) #Cannot be converted, does not belong to the basic requirements of numbers

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

 

2. Convert numeric values ​​to characters

E.g:

print(ord("X"))

print(chr(88))

 

Three, system conversion

E.g:

print(hex(200))

print(oct(200))

print(bin(200))

 

Demo:

QQ screenshot 20180423113355.png


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324714426&siteId=291194637