Detailed explanation of Python data type conversion (with detailed cases attached)

Posted by sarahk on Fri, 31 Dec 2021 18:10:23 +0100

Author home page: San Francisco wyx
This article has been entered into the column Introduction to Python
The latest version of Python Xiaobai tutorial in 2021 is for 0 basic Xiaobai and partners with weak foundation

Tip: click the blue "function name" in the list to go directly to the function parsing area

Function nameeffectuse
intConvert to integerint( value )
floatConvert to floating pointfloat( value )
strConvert to stringstr( value )
boolConvert to boolean typebool( value )
listConvert to listlist( value )
tupleConvert to Yuanzutuple( value )
setConvert to collectionset( value )

int()

Other data types can be converted to "integer" (int type) using the int() function

grammar

int( value )

parameter

  • value: data to be converted
  • Return value: converted data (int type)

Convert a float ing point type to an integer type, "take only integers", regardless of decimal places (no rounding)

float1 = 1.1
float2 = 1.9
print('When decimal places are less than 5:', int(float1))
print('When decimal places are greater than 5:', int(float2))

Output:

When decimal places are less than 5: 1
 When the decimal places are greater than 5: 1


The content is The string of "pure integer" can be converted to integer

For example, '111' can be transferred, and '111a' or '3.14' cannot be transferred

str1 = '111'
print('String with pure integer content:', int(str1))

Output:

String with pure integer content: 111

float()

Other data types can be converted to floating point (float type) using the float() function

grammar

float( value )

parameter

  • value: data of type to be converted
  • Return value: converted data (float type)

Convert integer (int type) to floating point

int1 = 111
print('Integer( int Type):', float(int1))

Output:

Integer( int Type): 111.0


The content is The string of "pure number" (integer or decimal) can be converted to floating point
str1 = '111'
str2 = '111.11'
print('String with pure integer content:', float(str1))
print('String with pure floating point content:', float(str2))

Output:

String with pure integer content: 111.0
 String with pure floating point content: 111.11

str()

Other data types can be converted to string type using str() function

grammar

str( value )

parameter

  • value: data of type to be converted
  • Return value: converted data (str type)

Most data types can be converted to string types

int1 = 111
float1 = 11.1
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}
dict = {'name': 'value'}
print('Integer:', str(int1))
print('Floating point:', str(float1))
print('List:', str(list1))
print('Yuanzu:', str(tuple1))
print('Set:', str(set1))
print('Dictionaries:', str(dict))

Output:

Integer: 111
 Floating point: 11.1
 List: [1, 2, 3]
Yuanzu: (1, 2, 3)
Set: {1, 2, 3}
Dictionaries: {'name': 'value'}

bool()

Other data types can be converted to Boolean types using the bool() function
grammar

bool( value )

parameter

  • value: data of type to be converted
  • Return value: converted data (bool type)

Null values (none, 0, "", (), [], {}) are converted to False, and other values are converted to True

None1 = None
int1 = 0
str1 = ""
list1 = []
tuple1 = ()
set1 = {}
print('None: ', bool(None1))
print('Number 0:', bool(int1))
print('Empty string:', bool(str1))
print('Empty list:', bool(list1))
print('Empty dictionary:', bool(tuple1))
print('Empty collection:', bool(set1))

Output:

None:  False
 Number 0: False
 Empty string: False
 Empty list: False
 Empty dictionary: False
 Empty collection: False

list()

Other data types can be converted to "list" (list type) using the list() function

grammar

list( value )

parameter

  • value: data to be converted to list type
  • Return value: converted data (list type)

"Iteratable objects" (str, tuple, set, dict) can be converted into lists

str1 = '111'
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}
print('character string:', list(str1))
print('Yuanzu:', list(tuple1))
print('Set:', list(set1))

Output:

character string: ['1', '1', '1']
Yuanzu: [1, 2, 3]
Set: [1, 2, 3]

tuple()

Other types of data can be converted to "list" (tuple type) using the tuple() function

grammar

tuple( value )

parameter

  • value: data to be converted
  • Return value: converted data (tuple type)

"Iteratable objects" (str, list, set, dict) can be transformed into Yuanzu

str1 = '111'
list1 = [1, 2, 3]
set1 = {1, 2, 3}
dict1 = {'name': 'value'}
print('character string:', tuple(str1))
print('List:', tuple(list1))
print('Yuanzu:', tuple(set1))
print('Dictionaries:', tuple(dict1))

Output:

character string: ('1', '1', '1')
List: (1, 2, 3)
Yuanzu: (1, 2, 3)
Dictionaries: ('name',)

set()

Other data types can be converted to "set" (set type) using the set() function

grammar

set( value )

parameter

  • value: data of type to be converted
  • Return value: converted data (set type)

Iteratable objects can be converted to collections

str1 = '111'
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
dict1 = {'name': 'value'}
print('character string:', set(str1))
print('List:', set(list1))
print('Yuanzu:', set(tuple1))
print('Dictionaries:', set(dict1))

Output:

character string: {'1'}
List: {1, 2, 3}
Yuanzu: {1, 2, 3}
Dictionaries: {'name'}

Topics: Python Operation & Maintenance Back-end Cyber Security