1. 程式人生 > >開始復習2

開始復習2

nag spl Go pre sdi lse rst sta false

數據類型:

字符串:

定義:在單引號、雙引號、三引號內,由一串字符組成。

name=‘egon‘

需要掌握:

1.索引取值(正向取,反向取) :只能取

2.切片(顧頭不顧尾,步長)

3.長度len

4.成員運算in和not in

5.移除空白strip

6.切分split

7.循環

需要掌握的操作:

1.strip,lstrip,rstrip

2.lower,upper

3.startwith,endwith

4.format的三種玩法

5.split,rsplit

6.join

7.replace

8.isdigit

name = ‘ egon  ‘
p = ‘**zzx***‘
print(name.lstrip(‘ ‘))
print(p.strip(‘*‘))
print(p.lstrip(‘*‘))
print(p.rstrip(‘*‘))


運行結果:
egon  
zzx
zzx***
**zzx

#lower upper


name = ‘ eGoN  ‘
print(name.lower())
print(name.upper())


運行結果:
egon  
EGON 

#startwith,endwith

name = ‘ eGoN  ‘
print(name.startswith(‘ ‘))
print(name.endswith(‘N‘))

運行結果:
True
False

#format 格式化輸出
# res = ‘{} {} {}‘.format(‘egon‘,‘18‘,‘male‘)
# res = ‘{1} {0} {1}‘.format(‘egon‘,‘18‘,‘male‘)
res = ‘name:{name}\nsex:{sex}\nage:{age}‘.format(sex = ‘male‘,age = 18,name = ‘egon‘)
print(res)


運行結果:
egon 18 male
18 egon 18
name:egon
sex:male
age:18

開始復習2