通过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')