1. 程式人生 > >for迴圈 字串的處理

for迴圈 字串的處理

# 1. for 迴圈 優點迴圈取值
# l=['a','b','c']
# print(len(l))#列印列表的長度即等於3
# i=0
# while i<len(l):
# print(l[i])
# i+=1
# l=['a','b','c']
# for item in l: #item='a'
# # print(item) 逐一取值賦值給item
dic={'x':111,'y':222,'z':333}
for k in dic: #k='x' 預設取得為字典裡面得key
print(k,dic[k]) #print自帶換行符 如果不需要換行則 print(,end='')
# # while迴圈和for迴圈對比
# # 1.while迴圈:稱之為條件迴圈,迴圈的次數取決於條件是否為Flase
# # 2.for迴圈:稱之為。。。迴圈,迴圈的次數取決於資料包含元素的個數
# #
# # for迴圈專門用來取值,再迴圈取值方面比while迴圈要強大
# #
# #
# # for+break
# # names=['egon','kevin','alex','hulaoshi']
# # for name in names:
# # if name == 'alex':break #遇alex後結束取值
# # print(name)
# # for+continue
# # names=['egon','kevin','alex','hulaoshi']
# # for name in names:
# # if name == 'alex':continue #只不取alex
# # print(name)
# # # 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): #i=2
# # # for j in range(2): #j=1
# # # print(i,j) #2,1
# #
# # range(3) 意思為範圍取值 0,1,2 取頭不取尾

# print(bin(12)) #十進位制轉二進位制
# print(oct(12))#十進位制轉八進位制
# print(hex(12))#十進位制轉十六進位制
# print(int('111111'))int可以輸入純數字字串
# print(float(10.1))


# 移除字串兩邊的字元
# msg='****hello***'
# print(msg.strip('*'))
# 從左邊開始直到遇到不是*的停止 ,右側從右邊開始直到不是*的停止
# info='egon:18:180:130'
# res=info.split(':',)
# print(res)
# s1=res[0]+':'+res[1]+':'+res[2]+':'+res[3]
# s1=''
# for item in res:
# s1+=item
# print(s1)
#
#
#
# s1=':'.join(res)
# print(s1)
# 九九乘法表
# for i in range(1,10):
# for j in range(1,i+1):
# print('%s*%s=%s' %(i,j,i*j),end=' ')
# print()
#