python学习之函数replace capitalize split zip map

一、字符串函数

  1、replace()

     函数原型S.replace(old, new[, count]) -> string

   比如 str="hello world hello phf" 

 str.replace("hello","hi")  则会将str里与world匹配的字符串用新的字符串 “hi”代替 输出"hi world hi phf"如果匹配不了原样输出

关于第三个参数 count 用来控制replace 前几个匹配的字符串 比如 str.replace("hello",'hi',1) 则只会用hi代替第一个匹配到的字符串,即输出 "hi world hello phf"


2、capitalize()

函数原型

  S.capitalize() -> string
    
    Return a copy of the string S with only its first character
    capitalized.

会将字符串S的第一个字符大写然后拷贝一份输出 比如str=“abc”

str.capitalize() 会输出 “Abc”

3、str.split()

 函数原型

S.split([sep [,maxsplit]]) -> list of strings

将字符串 以sep为分隔符 分片 如果给出maxsplit 则分片几次 比如

  ip="101.7.180.170"

ip.split('.') 输出["101","7","180","170"] 

ip.split('.',2)输出['101', '7', '180.170']



二 、 序列的函数 

   zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
    
    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.
 返回元祖的列表

 比如 

l1=[1,3,5,7,9]
>>> l2=[2,4,6,8,10]
>>> l3=zip(l1,l2)
>>> l3
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

如果两个序列长度不匹配 则会截断  即如果 l2=[2,4,6,8] 则会输出[(1, 2), (3, 4), (5, 6), (7, 8)]


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值