程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python data type conversion

編輯:Python

int,float transformation

# coding:utf-8:
if __name__ == '__main__':
''' example 1:int turn float Use float(int) float turn int Use int(float) '''
# example 1
a = 1.56
print(int(a)) # 1
b = 1
print(float(b)) # 1.0

int,string transformation

# coding:utf-8:
if __name__ == '__main__':
''' example 1:string turn int Use int(string). requirement string It can only consist of numbers , Otherwise, the report will be wrong int turn string Use str(int) '''
# example 1
a = '123'
print(int(a)) # 123
# print(int('123.0')) Error
# print(int('a123')) Error
b = 123
print(type(str(b))) # <class 'str'>

float,string transformation

# coding:utf-8:
if __name__ == '__main__':
''' example 1:float turn string Use str(float) string turn float Use float(string).string It can only consist of numbers or numbers plus a point , Otherwise, the report will be wrong '''
# example 1
a = 123.0
print(type(str(a))) # <class 'str'>
a = '123.01'
print(float(a)) # 123.01
# print(float('123.01.1')) Error
print(float('.123')) # 0.123
print(float('123.')) # 123.0
print(float('123')) # 123.0

string,list transformation

# coding:utf-8:
if __name__ == '__main__':
''' example 1:list(string) It can cut the string character by character into a list string.join(list) Be able to make list Each element is separated by string Concatenate to form a string example 2:string.split(sep=None,max_split = -1) Converts the string to... According to the specified characters and the number of cuts list sep: A cutting charm , The default is to cut space max_split: Number of cuts Default -1 Represent unlimited , Till the end of cutting '''
# example 1
print(list('abc')) # ['a', 'b', 'c']
print(''.join(['a', 'b', 'c'])) # 'abc'
print(','.join(['a', 'b', 'c'])) # 'a,b,c'
# example 2
print('a,b,c'.split(',', 1)) # ['a', 'b,c']
print('a,b,c'.split(',')) # ['a', 'b', 'c']
print('a b c'.split()) # ['a', 'b', 'c']

string,tuple transformation

# coding:utf-8:
if __name__ == '__main__':
''' example 1:string turn tuple Use tuple(string): Got tuple Consists of each character of a string tuple It can't be directly converted to string: You can turn first list, Retroversion string '''
# example 1
string = '12344'
t = tuple(string)
print(t) # ('1', '2', '3', '4', '4')
print(''.join(list(t))) # '12344'

tuple,list transformation

# coding:utf-8:
if __name__ == '__main__':
''' example 1:list turn tuple Use tuple(list) tuple turn list Use list(tuple) '''
# example 1
a = ['a', 'b', 'c']
t = tuple(a)
print(t) # ['a', 'b', 'c']
l = list(t)
print(l) # ['a', 'b', 'c']

convert to bool type

# coding:utf-8:
if __name__ == '__main__':
''' example 1:None,0, An empty string , empty list, empty tuple, empty dict, empty set Can be turned into False, Everything else can be changed into True '''
# example 1
a = 1
print(bool(a)) # True
print(bool(2)) # True
print(bool(0)) # False
print(bool(None)) # False
print(bool([])) # False
print(bool([1])) # True
print(bool(set())) # False
print(bool(set('1'))) # True
print(bool({
})) # False
print(bool({
'name': 'xie'})) # True
print(bool('')) # False
print(bool(' ')) # True
print(bool('0')) # True
print(bool('False')) # True

bool Convert other types

# coding:utf-8:
if __name__ == '__main__':
''' example 1:bool turn int:True=>1,False=>0 example 2:bool turn float:True=>1.0,False=>0.0 example 3:bool turn string:True=>'True',False=>'False' '''
a = True
b = False
# example 1
print(int(a)) # 1
print(int(b)) # 0
# example 2
print(float(a)) # 1.0
print(float(b)) # 0.0
# example 3
print(str(a)) # 'True'
print(str(b)) # 'False'

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved