Python 从零开始 string

通过Google Class学习Python,把学到的东西总结下来,希望对大家有用,如果有错误的地方,欢迎批评指正!
一、Python string注意点
1.变量无需声明
  s = 'hi'            ## 无需声明s
  print s[1]          ## i 无

  print len(s)        ## 2

  print s + ' there'  ## hi there
2、是str而不是string

 pi = 3.14

  ##text = 'The value of pi is ' + pi      ## NO, does not work

  text = 'The value of pi is '  + str(pi)  ## yes 利用str强制类型转换

二.str 常用函数
1、s.lower(), s.upper()   ##大小写转换 
2、s.isalpha()/s.isdigit()/s.isspace() ##是否全是字符/数字/空
3、s.startswith('other'), s.endswith('other') ##是否以“other”开头、结尾
4、s.find('other') ##返回s中‘other’的未知
5、s.replace('old', 'new') ##用‘new’替代‘old’
6、s.split('delim') ## 用delim分离出s
>>>s='aaa,bbb,ccc'.split(',')
>>>print s[0]
>>>aaa
7、s.join(list) 
>>>   '---'.join(['aaa', 'bbb', 'ccc'])
>>>   aaa---bbb---ccc

三、string操作符

String %

 text = ("%d little pigs come out or I'll %s and %s and %s" %

    (3, 'huff', 'puff', 'blow down')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值