常用python數(shù)據(jù)類型轉(zhuǎn)換函數(shù)總結(jié)
更新時(shí)間:2014年03月11日 15:14:14 作者:
這篇文章主要介紹了常用的python數(shù)據(jù)類型轉(zhuǎn)換函數(shù),并用實(shí)際例子說明了這些函數(shù)的用法,需要的朋友可以參考下
1、chr(i)
chr()函數(shù)返回ASCII碼對應(yīng)的字符串。
復(fù)制代碼 代碼如下:
>>> print chr(65)
A
>>> print chr(66)
>>> print chr(65)+chr(66)
AB
A
>>> print chr(66)
>>> print chr(65)+chr(66)
AB
2、complex(real[,imaginary])
complex()函數(shù)可把字符串或數(shù)字轉(zhuǎn)換為復(fù)數(shù)。
復(fù)制代碼 代碼如下:
>>> complex("2+1j")
(2+1j)
>>> complex("2")
(2+0j)
>>> complex(2,1)
(2+1j)
>>> complex(2L,1)
(2+1j)
3、float(x)
float()函數(shù)把一個(gè)數(shù)字或字符串轉(zhuǎn)換成浮點(diǎn)數(shù)。
復(fù)制代碼 代碼如下:
>>> float("12")
12.0
>>> float(12L)
12.0
>>> float(12.2)
12.199999999999999
12.0
>>> float(12L)
12.0
>>> float(12.2)
12.199999999999999
4、hex(x)
hex()函數(shù)可把整數(shù)轉(zhuǎn)換成十六進(jìn)制數(shù)。
復(fù)制代碼 代碼如下:
>>> hex(16)
'0x10'
>>> hex(123)
'0x7b'
'0x10'
>>> hex(123)
'0x7b'
5、long(x[,base])
long()函數(shù)把數(shù)字和字符串轉(zhuǎn)換成長整數(shù),base為可選的基數(shù)。
復(fù)制代碼 代碼如下:
>>> long("123")
123L
>>> long(11)
11L
123L
>>> long(11)
11L
6、list(x)
list()函數(shù)可將序列對象轉(zhuǎn)換成列表。如:
復(fù)制代碼 代碼如下:
>>> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list((1,2,3,4))
[1, 2, 3, 4]
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list((1,2,3,4))
[1, 2, 3, 4]
7、int(x[,base])
int()函數(shù)把數(shù)字和字符串轉(zhuǎn)換成一個(gè)整數(shù),base為可選的基數(shù)。
復(fù)制代碼 代碼如下:
>>> int(3.3)
3
>>> int(3L)
3
>>> int("13")
13
>>> int("14",15)
19
3
>>> int(3L)
3
>>> int("13")
13
>>> int("14",15)
19
8、min(x[,y,z...])
min()函數(shù)返回給定參數(shù)的最小值,參數(shù)可以為序列。
復(fù)制代碼 代碼如下:
>>> min(1,2,3,4)
1
>>> min((1,2,3),(2,3,4))
(1, 2, 3)
1
>>> min((1,2,3),(2,3,4))
(1, 2, 3)
9、max(x[,y,z...])
max()函數(shù)返回給定參數(shù)的最大值,參數(shù)可以為序列。
復(fù)制代碼 代碼如下:
>>> max(1,2,3,4)
4
>>> max((1,2,3),(2,3,4))
(2, 3, 4)
4
>>> max((1,2,3),(2,3,4))
(2, 3, 4)
10、oct(x)
oct()函數(shù)可把給出的整數(shù)轉(zhuǎn)換成八進(jìn)制數(shù)。
復(fù)制代碼 代碼如下:
>>> oct(8)
'010'
>>> oct(123)
'0173'
'010'
>>> oct(123)
'0173'
11、ord(x)
ord()函數(shù)返回一個(gè)字符串參數(shù)的ASCII碼或Unicode值。
復(fù)制代碼 代碼如下:
>>> ord("a")
97
>>> ord(u"a")
97
97
>>> ord(u"a")
97
12、str(obj)
str()函數(shù)把對象轉(zhuǎn)換成可打印字符串。
復(fù)制代碼 代碼如下:
>>> str("4")
'4'
>>> str(4)
'4'
>>> str(3+2j)
'(3+2j)'
'4'
>>> str(4)
'4'
>>> str(3+2j)
'(3+2j)'
13、tuple(x)
tuple()函數(shù)把序列對象轉(zhuǎn)換成tuple。
復(fù)制代碼 代碼如下:
>>> tuple("hello world")
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
>>> tuple([1,2,3,4])
(1, 2, 3, 4)
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
>>> tuple([1,2,3,4])
(1, 2, 3, 4)
14、type(x)
type()可以接收任何東西作為參數(shù)――并返回它的數(shù)據(jù)類型。整型、字符串、列表、字典、元組、函數(shù)、類、模塊,甚至類型對象都可以作為參數(shù)被 type 函數(shù)接受。
復(fù)制代碼 代碼如下:
>>> type(1)
<type 'int'>
>>> li = []
>>> type(li)
<type 'list'>
>>> import odbchelper
>>> type(odbchelper)
<type 'module'>
>>> import types
>>> type(odbchelper) == types.ModuleType
True
<type 'int'>
>>> li = []
>>> type(li)
<type 'list'>
>>> import odbchelper
>>> type(odbchelper)
<type 'module'>
>>> import types
>>> type(odbchelper) == types.ModuleType
True
您可能感興趣的文章:
- python輸入、數(shù)據(jù)類型轉(zhuǎn)換及運(yùn)算符方式
- python數(shù)據(jù)處理之Pandas類型轉(zhuǎn)換的實(shí)現(xiàn)
- Python數(shù)據(jù)類型轉(zhuǎn)換匯總
- Python數(shù)據(jù)類型轉(zhuǎn)換詳解
- Python如何實(shí)現(xiàn)強(qiáng)制數(shù)據(jù)類型轉(zhuǎn)換
- python3的數(shù)據(jù)類型及數(shù)據(jù)類型轉(zhuǎn)換實(shí)例詳解
- Python常見數(shù)據(jù)類型轉(zhuǎn)換操作示例
- 淺談python數(shù)據(jù)類型及類型轉(zhuǎn)換
- Python數(shù)據(jù)類型轉(zhuǎn)換實(shí)現(xiàn)方法
相關(guān)文章
Python網(wǎng)絡(luò)爬蟲實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Python網(wǎng)絡(luò)爬蟲實(shí)例,爬蟲的定義、主要框架等基礎(chǔ)概念,感興趣的小伙伴們可以參考一下2016-04-04python將圖片文件轉(zhuǎn)換成base64編碼的方法
這篇文章主要介紹了python將圖片文件轉(zhuǎn)換成base64編碼的方法,涉及Python操作base64編碼的技巧,需要的朋友可以參考下2015-03-03linux系統(tǒng)使用python獲取cpu信息腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python獲取cpu信息腳本,大家參考使用吧2014-01-01利用Python語言的grpc實(shí)現(xiàn)消息傳送詳解
gRPC是一個(gè)高性能、通用的開源RPC框架,其由Google主要面向移動應(yīng)用開發(fā)并基于HTTP/2協(xié)議標(biāo)準(zhǔn)而設(shè)計(jì)。本文主要介紹了如何利用Python語言的grpc實(shí)現(xiàn)消息傳送,感興趣的可以了解一下2023-03-03windows10安裝python依賴報(bào)錯(cuò)can‘t?create?or?remove?files?in?i
這篇文章主要介紹了windows10安裝python依賴報(bào)錯(cuò)can‘t?create?or?remove?files?in?install?directory問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助2023-09-09