python函數(shù)文字,python字符串函數(shù)

python3--內(nèi)置函數(shù)

python的常用內(nèi)置函數(shù)

在阿克陶等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站制作,阿克陶網(wǎng)站建設(shè)費(fèi)用合理。

1.abs() 函數(shù)返回?cái)?shù)字的絕對(duì)值

abs(-40)=40

2. dict() 函數(shù)用于創(chuàng)建一個(gè)字典

dict()

{} ? ? ?#創(chuàng)建一個(gè)空字典類似于u={},字典的存取方式一般為key-value

例如u = {"username":"tom", ?"age":18}

3. help() 函數(shù)用于查看函數(shù)或模塊用途的詳細(xì)說(shuō)明

help('math')查看math模塊的用處

a=[1,2,3,4]

help(a)查看列表list幫助信息

4.dir()獲得當(dāng)前模塊的屬性列表

dir(help)

['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

5.min() 方法返回給定參數(shù)的最小值 /參數(shù)可以為序列

a=? min(10,20,30,40)

a

10

6. next() 返回迭代器的下一個(gè)項(xiàng)目

it = iter([1, 2, 3, 4, 5])

next(it)

1

next(it)

2

7. id() 函數(shù)用于獲取對(duì)象的內(nèi)存地址

a=12

id(a)

1550569552

8.enumerate() 函數(shù)用于將一個(gè)可遍歷的數(shù)據(jù)對(duì)象(如列表、元組或字符串)組合為一個(gè)索引序列,同時(shí)列出數(shù)據(jù)和數(shù)據(jù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。

a=["tom","marry","leblan"]

list(enumerate(a))

[(0, 'tom'), (1, 'marry'), (2, 'leblan')]

9. oct() 函數(shù)將一個(gè)整數(shù)轉(zhuǎn)換成8進(jìn)制字符串

oct(15)

'0o17'

oct(10)

'0o12'

10. bin() 返回一個(gè)整數(shù) int 或者長(zhǎng)整數(shù) long int 的二進(jìn)制表示

bin(10)

'0b1010'

bin(15)

'0b1111'

11.eval() 函數(shù)用來(lái)執(zhí)行一個(gè)字符串表達(dá)式,并返回表達(dá)式的值

eval('2+2')

4

12.int() 函數(shù)用于將一個(gè)字符串會(huì)數(shù)字轉(zhuǎn)換為整型

int(3)

3

int(3.6)

3

int(3.9)

3

int(4.0)

4

13.open() 函數(shù)用于打開一個(gè)文件,創(chuàng)建一個(gè)file對(duì)象,相關(guān)的方法才可以調(diào)用它進(jìn)行讀寫

f=open('test.txt')

14.str() 函數(shù)將對(duì)象轉(zhuǎn)化為適于人閱讀的形式

str(3)

'3'

15. bool() 函數(shù)用于將給定參數(shù)轉(zhuǎn)換為布爾類型,如果沒(méi)有參數(shù),返回 False

bool()

False

bool(1)

True

bool(10)

True

bool(10.0)

True

16.isinstance() 函數(shù)來(lái)判斷一個(gè)對(duì)象是否是一個(gè)已知的類型

a=5

isinstance(a,int)

True

isinstance(a,str)

False

17. sum() 方法對(duì)系列進(jìn)行求和計(jì)算

sum([1,2,3],5)

11

sum([1,2,3])

6

18. super() 函數(shù)用于調(diào)用下一個(gè)父類(超類)并返回該父類實(shí)例的方法。super 是用來(lái)解決多重繼承問(wèn)題的,直接用類名調(diào)用父類方法

class ? User(object):

? def__init__(self):

class Persons(User):

? ? ? ? super(Persons,self).__init__()

19. float() 函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點(diǎn)數(shù)

float(1)

1.0

float(10)

10.0

20. iter() 函數(shù)用來(lái)生成迭代器

a=[1,2,3,4,5,6]

iter(a)

for i in iter(a):

... ? ? ? ? print(i)

...

1

2

3

4

5

6

21.tuple 函數(shù)將列表轉(zhuǎn)換為元組

a=[1,2,3,4,5,6]

tuple(a)

(1, 2, 3, 4, 5, 6)

22.len() 方法返回對(duì)象(字符、列表、元組等)長(zhǎng)度或項(xiàng)目個(gè)數(shù)

s = "playbasketball"

len(s)

14

a=[1,2,3,4,5,6]

len(a)

6

23. property() 函數(shù)的作用是在新式類中返回屬性值

class User(object):

?def __init__(self,name):

? ? ? ? ? self.name = name

def get_name(self):

? ? ? ? ? return self.get_name

@property

?def name(self):

? ? ? ? ?return self_name

24.type() 函數(shù)返回對(duì)象的類型

25.list() 方法用于將元組轉(zhuǎn)換為列表

b=(1,2,3,4,5,6)

list(b)

[1, 2, 3, 4, 5, 6]

26.range() 函數(shù)可創(chuàng)建一個(gè)整數(shù)列表,一般用在 for 循環(huán)中

range(10)

range(0, 10)

range(10,20)

range(10, 20)

27. getattr() 函數(shù)用于返回一個(gè)對(duì)象屬性值

class w(object):

... ? ? ? ? ? ? s=5

...

a = w()

getattr(a,'s')

5

28. complex() 函數(shù)用于創(chuàng)建一個(gè)復(fù)數(shù)或者轉(zhuǎn)化一個(gè)字符串或數(shù)為復(fù)數(shù)。如果第一個(gè)參數(shù)為字符串,則不需要指定第二個(gè)參數(shù)

complex(1,2)

(1+2j)

complex(1)

(1+0j)

complex("1")

(1+0j)

29.max() 方法返回給定參數(shù)的最大值,參數(shù)可以為序列

b=(1,2,3,4,5,6)

max(b)

6

30. round() 方法返回浮點(diǎn)數(shù)x的四舍五入值

round(10.56)

11

round(10.45)

10

round(10.45,1)

10.4

round(10.56,1)

10.6

round(10.565,2)

10.56

31. delattr 函數(shù)用于刪除屬性

class Num(object):

...? ? a=1

...? ? b=2

...? ? c=3.

.. print1 = Num()

print('a=',print1.a)

a= 1

print('b=',print1.b)

b= 2

print('c=',print1.c)

c= 3

delattr(Num,'b')

print('b=',print1.b)

Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'

32. hash() 用于獲取取一個(gè)對(duì)象(字符串或者數(shù)值等)的哈希值

hash(2)

2

hash("tom")

-1675102375494872622

33. set() 函數(shù)創(chuàng)建一個(gè)無(wú)序不重復(fù)元素集,可進(jìn)行關(guān)系測(cè)試,刪除重復(fù)數(shù)據(jù),還可以計(jì)算交集、差集、并集等。

a= set("tom")

b = set("marrt")

a,b

({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})

ab#交集

{'t', 'm'}

a|b#并集

{'t', 'm', 'r', 'o', 'a'}

a-b#差集

{'o'}

python中函數(shù)定義

1、函數(shù)定義

①使用def關(guān)鍵字定義函數(shù)

def 函數(shù)名(參數(shù)1.參數(shù)2.參數(shù)3...):

"""文檔字符串,docstring,用來(lái)說(shuō)明函數(shù)的作用"""

#函數(shù)體

return 表達(dá)式

注釋的作用:說(shuō)明函數(shù)是做什么的,函數(shù)有什么功能。

③遇到冒號(hào)要縮進(jìn),冒號(hào)后面所有的縮進(jìn)的代碼塊構(gòu)成了函數(shù)體,描述了函數(shù)是做什么的,即函數(shù)的功能是什么。Python函數(shù)的本質(zhì)與數(shù)學(xué)中的函數(shù)的本質(zhì)是一致的。

2、函數(shù)調(diào)用

①函數(shù)必須先定義,才能調(diào)用,否則會(huì)報(bào)錯(cuò)。

②無(wú)參數(shù)時(shí)函數(shù)的調(diào)用:函數(shù)名(),有參數(shù)時(shí)函數(shù)的調(diào)用:函數(shù)名(參數(shù)1.參數(shù)2.……)

③不要在定義函數(shù)的時(shí)候在函數(shù)體里面調(diào)用本身,否則會(huì)出不來(lái),陷入循環(huán)調(diào)用。

④函數(shù)需要調(diào)用函數(shù)體才會(huì)被執(zhí)行,單純的只是定義函數(shù)是不會(huì)被執(zhí)行的。

⑤Debug工具中Step into進(jìn)入到調(diào)用的函數(shù)里,Step Into My Code進(jìn)入到調(diào)用的模塊里函數(shù)。

python字典操作函數(shù)

字典是一種通過(guò)名字或者關(guān)鍵字引用的得數(shù)據(jù)結(jié)構(gòu),其鍵可以是數(shù)字、字符串、元組,這種結(jié)構(gòu)類型也稱之為映射。字典類型是Python中唯一內(nèi)建的映射類型,基本的操作包括如下:

(1)len():返回字典中鍵—值對(duì)的數(shù)量;

(2)d[k]:返回關(guān)鍵字對(duì)于的值;

(3)d[k]=v:將值關(guān)聯(lián)到鍵值k上;

(4)del d[k]:刪除鍵值為k的項(xiàng);

(5)key in d:鍵值key是否在d中,是返回True,否則返回False。

(6)clear函數(shù):清除字典中的所有項(xiàng)

(7)copy函數(shù):返回一個(gè)具有相同鍵值的新字典;deepcopy()函數(shù)使用深復(fù)制,復(fù)制其包含所有的值,這個(gè)方法可以解決由于副本修改而使原始字典也變化的問(wèn)題

(8)fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認(rèn)對(duì)應(yīng)的值為None

(9)get函數(shù):訪問(wèn)字典成員

(10)has_key函數(shù):檢查字典中是否含有給出的鍵

(11)items和iteritems函數(shù):items將所有的字典項(xiàng)以列表方式返回,列表中項(xiàng)來(lái)自(鍵,值),iteritems與items作用相似,但是返回的是一個(gè)迭代器對(duì)象而不是列表

(12)keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

(13)pop函數(shù):刪除字典中對(duì)應(yīng)的鍵

(14)popitem函數(shù):移出字典中的項(xiàng)

(15)setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值

(16)update函數(shù):用一個(gè)字典更新另外一個(gè)字典

(17)?values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復(fù)的元素

一、字典的創(chuàng)建

1.1 直接創(chuàng)建字典

d={'one':1,'two':2,'three':3}

printd

printd['two']

printd['three']

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1.2 通過(guò)dict創(chuàng)建字典

# _*_ coding:utf-8 _*_

items=[('one',1),('two',2),('three',3),('four',4)]

printu'items中的內(nèi)容:'

printitems

printu'利用dict創(chuàng)建字典,輸出字典內(nèi)容:'

d=dict(items)

printd

printu'查詢字典中的內(nèi)容:'

printd['one']

printd['three']

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

items中的內(nèi)容:

[('one',1), ('two',2), ('three',3), ('four',4)]

利用dict創(chuàng)建字典,輸出字典內(nèi)容:

{'four':4,'three':3,'two':2,'one':1}

查詢字典中的內(nèi)容:

或者通過(guò)關(guān)鍵字創(chuàng)建字典

# _*_ coding:utf-8 _*_

d=dict(one=1,two=2,three=3)

printu'輸出字典內(nèi)容:'

printd

printu'查詢字典中的內(nèi)容:'

printd['one']

printd['three']

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出字典內(nèi)容:

{'three':3,'two':2,'one':1}

查詢字典中的內(nèi)容:

二、字典的格式化字符串

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

print"three is %(three)s."%d

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

threeis3.

三、字典方法

3.1?clear函數(shù):清除字典中的所有項(xiàng)

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

d.clear()

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

{}

請(qǐng)看下面兩個(gè)例子

3.1.1

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{'two':2,'one':1}

3.1.2

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d.clear()

printd

printdd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{}

3.1.2與3.1.1唯一不同的是在對(duì)字典d的清空處理上,3.1.1將d關(guān)聯(lián)到一個(gè)新的空字典上,這種方式對(duì)字典dd是沒(méi)有影響的,所以在字典d被置空后,字典dd里面的值仍舊沒(méi)有變化。但是在3.1.2中clear方法清空字典d中的內(nèi)容,clear是一個(gè)原地操作的方法,使得d中的內(nèi)容全部被置空,這樣dd所指向的空間也被置空。

3.2?copy函數(shù):返回一個(gè)具有相同鍵值的新字典

# _*_ coding:utf-8 _*_

x={'one':1,'two':2,'three':3,'test':['a','b','c']}

printu'初始X字典:'

printx

printu'X復(fù)制到Y(jié):'

y=x.copy()

printu'Y字典:'

printy

y['three']=33

printu'修改Y中的值,觀察輸出:'

printy

printx

printu'刪除Y中的值,觀察輸出'

y['test'].remove('c')

printy

printx

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

初始X字典:

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

X復(fù)制到Y(jié):

Y字典:

{'test': ['a','b','c'],'one':1,'three':3,'two':2}

修改Y中的值,觀察輸出:

{'test': ['a','b','c'],'one':1,'three':33,'two':2}

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

刪除Y中的值,觀察輸出

{'test': ['a','b'],'one':1,'three':33,'two':2}

{'test': ['a','b'],'three':3,'two':2,'one':1}

注:在復(fù)制的副本中對(duì)值進(jìn)行替換后,對(duì)原來(lái)的字典不產(chǎn)生影響,但是如果修改了副本,原始的字典也會(huì)被修改。deepcopy函數(shù)使用深復(fù)制,復(fù)制其包含所有的值,這個(gè)方法可以解決由于副本修改而使原始字典也變化的問(wèn)題。

# _*_ coding:utf-8 _*_

fromcopyimportdeepcopy

x={}

x['test']=['a','b','c','d']

y=x.copy()

z=deepcopy(x)

printu'輸出:'

printy

printz

printu'修改后輸出:'

x['test'].append('e')

printy

printz

運(yùn)算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出:

{'test': ['a','b','c','d']}

{'test': ['a','b','c','d']}

修改后輸出:

{'test': ['a','b','c','d','e']}

{'test': ['a','b','c','d']}

3.3?fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認(rèn)對(duì)應(yīng)的值為None

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'])

printd

運(yùn)算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':None,'two':None,'one':None}

或者指定默認(rèn)的對(duì)應(yīng)值

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'],'unknow')

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':'unknow','two':'unknow','one':'unknow'}

3.4?get函數(shù):訪問(wèn)字典成員

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.get('one')

printd.get('four')

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1

None

注:get函數(shù)可以訪問(wèn)字典中不存在的鍵,當(dāng)該鍵不存在是返回None

3.5?has_key函數(shù):檢查字典中是否含有給出的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.has_key('one')

printd.has_key('four')

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

True

False

3.6?items和iteritems函數(shù):items將所有的字典項(xiàng)以列表方式返回,列表中項(xiàng)來(lái)自(鍵,值),iteritems與items作用相似,但是返回的是一個(gè)迭代器對(duì)象而不是列表

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

list=d.items()

forkey,valueinlist:

printkey,':',value

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

three :3

two :2

one :1

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

it=d.iteritems()

fork,vinit:

print"d[%s]="%k,v

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

d[three]=3

d[two]=2

d[one]=1

3.7?keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printu'keys方法:'

list=d.keys()

printlist

printu'\niterkeys方法:'

it=d.iterkeys()

forxinit:

printx

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

keys方法:

['three','two','one']

iterkeys方法:

three

two

one

3.8?pop函數(shù):刪除字典中對(duì)應(yīng)的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.pop('one')

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'three':3,'two':2}

3.9?popitem函數(shù):移出字典中的項(xiàng)

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.popitem()

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'two':2,'one':1}

3.10?setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.setdefault('one',1)

printd.setdefault('four',4)

printd

運(yùn)算結(jié)果:

{'three':3,'two':2,'one':1}

{'four':4,'three':3,'two':2,'one':1}

3.11?update函數(shù):用一個(gè)字典更新另外一個(gè)字典

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3

}

printd

x={'one':1}

d.update(x)

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':123}

{'three':3,'two':2,'one':1}

3.12?values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復(fù)的元素

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3,

'test':2

}

printd.values()

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

[2,3,2,123]

python之字符串內(nèi)置函數(shù)

1. 字符串字母處理

2. 字符串填充

str.ljust(width, fillchar)、str.center(width, fillchar)、str.rjust(width, fillchar)

返回一個(gè)指定的寬度 width 「居左」/「居中」/「居右」的字符串,如果 width 小于字符串寬度直接返回字符串,否則使用 fillchar 去填充。

3,字符串計(jì)數(shù)

str.count(sub, start, end)

#統(tǒng)計(jì)字符串里某個(gè)字符出現(xiàn)的次數(shù)??蛇x參數(shù)為在字符串搜索的開始與結(jié)束位置。

start, end遵循**“左閉右開”**原則。

4. 字符串位置

str.endswith(suffix, start, end)和str.startswith(substr, beg, end)

#判斷字符串是否以指定后綴結(jié)尾/開頭,如果以指定后綴「結(jié)尾」/「開頭」返回 True,否則返回 False。

5. 字符串查找

6. 字符串判斷

7. 字符串拼接

str.join() #將序列中的元素以指定的字符連接生成一個(gè)新的字符串。

s1 = "-" s2 = "" seq = ("r", "u", "n", "o", "o", "b")

# 字符串序列 print (s1.join( seq )) print (s2.join( seq )) r-u-n-o-o-b runoob

8. 統(tǒng)計(jì)字符串長(zhǎng)度

str.len() #返回對(duì)象(字符、列表、元組等)長(zhǎng)度或項(xiàng)目個(gè)數(shù)。

9. 去除字符兩側(cè)空格

str.lstrip()、str.rstrip()、str.strip() #截掉字符串「左邊」/「右邊」/「左右」兩側(cè)的空格或指定字符。

str0 = ' Hello World!' str0.lstrip() 'Hello World!' str1 = 'aaaa Hello World!' str1.lstrip('a') ' Hello World!'

10. str.maketrans(intab, outtab)和str.translate(table)

str.maketrans()創(chuàng)建字符映射的轉(zhuǎn)換表

str.maketrans()根據(jù)參數(shù)table給出的表轉(zhuǎn)換字符串的字符。

str.maketrans()傳入的也可以是字典

tab = {'e': '3', 'o': '4'} trantab = str.maketrans(tab) str0.translate(trantab) 'H3ll4 W4rld!'

11. 字符串替換

str.replace(old, new, max)

12. 字符分割

str.split(str, num)

13. 字符填充

str.zfill(width)

返回指定長(zhǎng)度的字符串,原字符串右對(duì)齊,前面填充0。

當(dāng)前標(biāo)題:python函數(shù)文字,python字符串函數(shù)
鏈接URL:http://muchs.cn/article10/phiogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)網(wǎng)站改版、虛擬主機(jī)用戶體驗(yàn)、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開發(fā)