s = 'ABCDEFGHIJKLMN'
s1 = s[0]
print('s[0] = ' + s1) #s[0] = A
s = 'ABCDEFGHIJKLMN'
s2 = s[0:3]
print('s[0:3] = ' + s2) #s[0:3] = ABC
s3 = 'ABCDEFGHIJKLMN'
print(s3[0:6:2]) #ACE
s = 'alexWUsir'
s4_1 = s.capitalize() #首字母大寫
print(s4_1) #Alexwusir
s = 'alexWUsir'
s4_2 = s.upper() #所有大寫
print(s4_2) #ALEXWUSIR
s = 'alexWUsir'
s4_3 = s.lower() #所有小寫
print(s4_3) #alexwusir
s = 'alexWUsir'
s4_4 = s.swapcase() #大小寫互換
print(s4_4) #ALEXwuSIR
s = 'xc——gx*zs_shy+hihn9bbklv yiu'
s7 = s.title()
print(s7) #Xc——Gx*Zs_Shy+Hihn9Bbklv Yiu
#s.find經過元素找索引,找到返回索引,找不到返回-1
#s.index經過元素找索引,找到返回索引,找不到返回error
s = 'alexWUsir'
s8_11 = s.find('W')
s8_12 = s.index('W')
s8_21 = s.find('WU')
s8_22 = s.index('WU')
s8_31 = s.find('A')
s8_32 = s.index('A')
print(s8_11,type(s8_12)) #4 <class 'int'>
print(s8_21 ,type(s8_22)) #4 <class 'int'>
print(s8_31 ,type(s8_32)) #報錯:ValueError: substring not found----未找到子字符串
s = ' alexW%Usir %2% '
s9_1 = s.strip() #刪除字符串先後的空格
print(s9_1) #alexW%Usir %2%
ss = '% alexW%Usir %2% %'
s9_2 = ss.strip('%') #刪除字符串先後的%
print(s9_2) # alexW%Usir %2%
username = input('張三 ').strip()
if username == '張三':
print('你好呀 主人')
s = 'alexaa wusirl'
s10 = s.count('a')
print('此字符串中有' + s10 + '個a') #報錯:TypeError: must be str, not int
print('此字符串中有' + str(s10) + '個a') #此字符串中有3個a
s = 'alex wusir taibai'
s1 = 'ale:x wus:ir :taibai'
s11_1 = s.split()
print(s11_1) #['alex', 'wusir', 'taibai']
s11_2 = s1.split(':')
print(s11_2) #['ale', 'x wus', 'ir ', 'taibai']
s12_1 = '我叫{},今年{}歲,愛好{},再說一下我叫{}'.format('張三',23,'學習','張三')
print(s12_1) #我叫張三,今年23歲,愛好學習,再說一下我叫張三
s12_2 = '我叫{0},今年{1}歲,愛好{2},再說一下我叫{0}'.format('張三',23,'學習')
print(s12_2) #我叫張三,今年23歲,愛好學習,再說一下我叫張三
s12_3 = s1 = '我叫{name},今年{age}歲,愛好{hobby},再說一下我叫{name}'.format(name = '張三',age = 23,hobby = '學習')
print(s12_3) #我叫張三,今年23歲,愛好學習,再說一下我叫張三
s13_1 = '張三,哈嘍你好,我是張三'
s13_2 = s13_1.replace('張三','李四')
s13_3 = s13_1.replace('張三','李四',1)
print(s13_1) #張三,哈嘍你好,我是張三----原來文本
print(s13_2) #李四,哈嘍你好,我是李四----全都替換
print(s13_3) #李四,哈嘍你好,我是張三----只替換第一個
s = 'zhangsan'
for i in s:
print(i)
a = [1,2,3]
a.append(4) #the result : [1, 2, 3, 4]
a = [1,2,3]
a.insert(0,'aa') #the result : ['aa', 1, 2, 3]
a = [1,2,3]
b = [4,5,6]
a.extend(b) #the result :[1, 2, 3, 4, 5, 6]
a = ['aa','bb','cc','aa','aa']
print(a.count('aa')) #the result : 3
a = [1,2,3]
a.pop() #the result : [1, 2]
a.pop(0)
a = ['aa','bb','cc','aa']
a.remove('aa') #the result : ['bb', 'cc', 'aa']
a = ['a','b','c']
a.reverse() #the result : ['c', 'b', 'a']
a = ['a','b','c',1,2,3]
a.sort() #the result :[1, 2, 3, 'a', 'b', 'c']
a = [1,2,3,1]
print(a.index(1)) #the result : 0
li = [11,22,33]
for k,v in enumerate(li, 1):
print(k,v) #the result : 1 11 2 22 3 33
tup1=(50,)
tup1=(12,34.56)
tup2=('abc','xyz')
tup1[0]=100 #非法操做,元組中的元素是不容許被修改的
tup3=tup1+tup2 #python運行元組進行鏈接組合
print(tup3)# 輸出:(12,34.56,'abc','xyz')
tup1=(1,2,3)
tup2=(3,4,5)
tup3=tup1+tup2 #輸出:tup3=(1,2,3,3,4,5)
tup4=tup1*3 #輸出: tup4=(1,2,3,1,2,3,1,2,3)
a=1,2,3,'hello'
print(a) #輸出:(1,2,3,'hello')
——cmp(tup1,tup2): 比較兩個元組元素 ——len(tup): 返回元組中元素的個數 ——max(tup): 返回元組中元素最大的值 ——min(tup): 返回元組中元素最小的值 ——tuple(seq): 將列表轉化爲元組
——tuple.index(obj):從元組中找出某個值第一個匹配項的索引值 ——tuple.count(obj): 統計某個元素在元組中出現的次數
d = {'name':"tom"}
d.clear()
print(d) #the result : {}
d = {'Tom':8777,'Jack':8888,'Fly':6666}
print(d.get('Tom')) #the result : 8777
print(d.get('not_exist')) #the result : None
d = {'Tom':8777,'Jack':8888,'Fly':6666}
a = {'Tom':110,'Test':119}
d.update(a)
print(d) #the result :{'Fly': 6666, 'Test': 119, 'Jack': 8888, 'Tom': 110}
d = {'Tom':8777,'Jack':8888,'Fly':6666}
d.setdefault('Tom') #the result : 8777
print(d.setdefault('Test')) #the result : None
print(d) #{'Fly': 6666, 'Jack': 8888, 'Tom': 8777, 'Test': None}
d = {'Tom':8777,'Jack':8888,'Fly':6666}
v = d.pop('Tom')
print(v) #8777
print({}.fromkeys(['name','age'])) #the result : {'age': None, 'name': None}
d = {'Tom':8777,'Jack':8888,'Fly':6666}
for k,v in d.items():
print(k,v)
for k in d.values():
print(k)
for k in d.keys():
print(k)
keys = ['a', 'b']
values = [1, 2]
print(dict(zip(keys,values))) # {'a': 1, 'b': 2}