python笔记-3

上一篇 / 下一篇  2012-06-04 19:10:18 / 个人分类:Python笔记

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 '


TAG: Python python

 

评分:0

我来说两句

Open Toolbar