python学习笔记(3)

发布时间:2020-09-03编辑:脚本学堂
python学习笔记(3)
字符串函数、位置参数等。
1、位置参数
复制代码 代码如下:
bucky="Hey there %s,hows your %s"
varb=('betty','foot')
print bucky % varb   --->>Hey there betty,hows your foot

2、查找字符串的第一个字母所在位置
复制代码 代码如下:
example="Hey now bessie nice chops"
example
'Hey now bessie nice chops'
>>> example.find('bessie')---->8
>>> example.find('Hey')--->0

3、join函数--->>插入到字符串后面
复制代码 代码如下:
>>> sequence=['hey','there','bessie','hoss']
>>> sequence
['hey', 'there', 'bessie', 'hoss']
>>> glue='hoss'
glue.join(sequence)
'heyhosstherehossbessiehosshoss'
4、lower()函数
复制代码 代码如下:
randstr="I wish i Had NO sausage"
>>> randstr
'I wish i Had NO sausage'
>>> randstr.lower()--->>'i wish i had no sausage'

5、replace()字符串的替换
复制代码 代码如下:
truth="I love old women"
>>> truth
'I love old women'
>>> truth.replace('women','men')--->>'I love old men'
6、sort()排序
new=[32,54,22,7,98,1]
>>> new
[32, 54, 22, 7, 98, 1]
>>> new.sort()
new----->>[1, 7, 22, 32, 54, 98]

7、
复制代码 代码如下:
sorted('Easyhoss')
['E', 'a', 'h', 'o', 's', 's', 's', 'y']
41,42,32,54
(41, 42, 32, 54)

8、
复制代码 代码如下:
>>> bucky=(32,32,43,54)
>>> bucky[2]
43
 

9、if...elif...else
复制代码 代码如下:
fish="tuna"
if fish=="bass":
    print 'this is a fish alright'
elif fish=="slamon":
    print "I hope i dont get SLAMonilla poissnin"
elif fish=="tuna":
    print "easy there tuna boat"
else:
    print 'I dont know what this is'

10、if..if..else..else
复制代码 代码如下:
if thing == "animal":
    if animal == "cat":
        print'this is a cat'
    else:
        print 'i dont know this animal is '
else:
    print 'i dont know what this thing is '