1. 程式人生 > >python學習day6 for迴圈 字串的內建方法

python學習day6 for迴圈 字串的內建方法

1.for迴圈

和while相比

l=[1,2,3]

i=0

while i <len(l)

  print(l[i])

  i+=1

 

l=['a','b','c']

for item in l:

  print(item)

字典中的應用:

  dic={'x':111,'y':222,'z':333}

  for k in dic:

    print(k,dic[k])

 

while迴圈稱為條件迴圈,迴圈的次數取決於條件何時為false

for稱為迭代器迴圈,迴圈的次數取決於資料包含的元素的個數

 

2.for迴圈專門用來取值,在迴圈方面比while迴圈強大,以後凡是遇到迴圈取值的場景,就應該用for迴圈

l=[1,2,3]

for i in range(3)

  print(i,l[i])

 

for+break

  names=['egon','kevin','alex','hulaoshi']

  for name in names:

    if name == 'alex':break

    print(name)

for+continue

  names=['egon','kevin','alex','hulaoshi']

  for name in names:

    if name == 'alex':continue

    print(name)

 

for+else 和while else相似,只有for迴圈沒有被break結束的情況下會執行else內的程式碼

# for+else
# names=['egon','kevin','alex','hulaoshi']
# for name in names:
# # if name == 'alex':break
# print(name)
# else:
# print('=====>')

for迴圈的巢狀
  for i in range(3):
    for j in range(2):
      print(i,j)

print預設在結尾會換行,可以使用print(‘ ’,end='')使得輸入內容後不換行

2.資料型別補充
整型int
定義 age=10 age=int(10)
型別轉換
int(3.1)
int('3333')

特點:不可變型別
存一個值

浮點型float
float('11111.1)
特點:不可變型別
存一個值
x=11.1
id(x)
x=11.2
id(x)

瞭解內容**
十進位制轉換成其他進位制
bin(13)
oct(13)八進位制
hex(13)十六進位制

3.字串型別str:用來記錄描述性的狀態,比如名字,性別等
型別轉換
res1=str(10)
res2=str(22.2)
res3=str(['a','b'])
res4=str({'x'}:1,'y':2)
type(res4)

   3.2常用操作+內建方法
  優先掌握(*****)
  1.按索引取值(正向取+反向取):只能取字元,不能改
  msg='hello world'
  print(msg[-1])
  msg[0]='H'(會報錯)
  2.切片(顧頭不顧尾,步長)
  msg=‘hello world'
  print(msg[0:5])
  print(msg[0:5:2])從第一位到第五位字元,隔一個取一個
  print(msg[0:])從頭取到尾
  print(mag[:])
  反向取
  print(msg[-1:-5:-1])從最後一位反向取到倒數第四位
  print(msg[::-1])反轉字串
  3.長度len:統計的是字元的個數
  msg=‘h長a’
  len(msg)
  4.成員運算子 in not in 判斷一個子字元是否存在於一個大字串中
  msg='hello world'
  print('ho' in msg)
  print('ho' not in msg)
  5.移除空白strip:移除字串左右兩邊的某些字元
  msg=‘ hello world '
  print(msg.strip(' '))
  print(msg.strip())
  print(msg)(不直接修改字串)
 
# name=input('name>>>: ').strip() #name='egon'
# pwd=input('password>>>: ').strip()
#
# if name == 'egon' and pwd == '123':
# print('login successfull')
# else:
# print('username or password error')

# msg='***h**ello**********'
# print(msg.strip('*'))

# msg='*-=+h/ello*(_+__'
# print(msg.strip('*-=+/(_'))

6.切分split:把有規律的字串切成列表從而方便取值
info=‘egon:18:180:150’
res=info.split(‘:’,1)
print(res)
print(res[1])

s1=':'join(res)
s1=res[0]+':'+res[1]+':'+res[2]+....
':'.join([1,2,3,4,5]) 會報錯,符號和數字沒法相加
7.迴圈
  for i in 'hello':
    print(i)

需要掌握的操作(****)
1.strip,lstrip,rstrip
msg='****hello*****'
msg.strip('*')兩邊都去除*
msg.lstrip('*')從左邊開始去除*
msg.rstrip('*')從右邊開始去除*
2.lower,upper
msg='AaBbCcDdEe'
msg.lower()全部轉換成小寫
msg.upper()全部轉換成大寫
3.starswith,endswith
msg='alex is sb'
msg.startswith('al')字串是否是al開頭
msg.endswith('sb')字串是否sb結尾,返回布林值
4.format的三種玩法
msg=‘my name is %s,my age is %s'%('egon',18)
print(msg)
msg=‘my name is {name},my age is {age}'.format(name='egon',age=18)
msg=‘my name is {},my age is {}'.format('egon',18)
msg=‘my name is {0}{0},my age is {1}{1}{1}'.format('egon',18) 可以按數字對應後續的變數,多次引用變數
5.split,rsplit
cmd='get|a.txt|333'
print(cmd.split('|',1))
print(cmd.rsplit('|',1))從右開始分割字串
6.replace
msg=‘kevin is sb kevin kevin'
print(msg.replace('kevin','sb',2))
7.isdigit 當字串中為純數字時結果為true
res=‘11111’(有小數點也不算是純數字)
print(res.isdigit())
int(res)
# age_of_bk=18
# inp_age=input('your age: ').strip()
# if inp_age.isdigit():
# inp_age=int(inp_age)
# if inp_age > 18:
# print('too big')
# elif inp_age < 18:
# print('to small')
# else:
# print('you got it')
# else:
# print('必須輸入純數字')
瞭解的內容(**)
1.find,rfind,index,rindex取到搜尋的值的索引值,找到返回索引值,沒找到find返回-1,index報錯
# print('xxxkevin is sb kevin'.find('kevin'))
# print('xxxkevin is sb kevin'.index('kevin'))
# print('xxxkevin is sb kevin'.rfind('kevin'))
# print('xxxkevin is sb kevin'.rindex('kevin'))

2.count,計運算元字串在大字串中出現幾次
# print('kevin is kevin is kevin is sb'.count('kevin'))
3.center,ljust,rjust,zfill 用字元填補空白
# print('egon'.center(50,'*'))居中顯示‘egon’,總共顯示50個字元,空白用*號填補
# print('egon'.ljust(50,'*')左對齊顯示,空白用*顯示

# print('egon'.rjust(50,'*'))
# print('egon'.zfill(50))左邊用0填補

#4、captalize,swapcase,title
# print('my name is kevin'.capitalize())把字串第一個字母改成大寫
# print('AaBbCc'.swapcase())大寫變小寫,小寫變大寫
# print('my name is kevin'.title())把每個單詞的首字母大寫

#4、is其他
# name='egon123'
# print(name.isalnum()) #判斷字串由字母或數字組成
# print(name.isalpha()) #判斷字串只由字母組成

# print(name.islower())是否全小寫
# print(name.isupper())
# name=' '
# print(name.isspace())是否是空格
msg='I Am Egon'
print(msg.istitle())是否每個單詞首字母大寫