1.字符串截取:
1.1 從某個子字符串的位置截取:
如:
#上海新國際博覽中心 乘車路線 全景地圖上海新國際博覽中心
#截取後
#上海新國際博覽中心
a='上海新國際博覽中心 乘車路線 全景地圖上海新國際博覽中心'
a[:a.index('乘車路線')]
截取最後的部分:
#主辦單位:中國日用雜品工業協會;承辦單位:北京匯捷通國際展覽有限公司
#截取後
#北京匯捷通國際展覽有限公司
b='主辦單位:中國日用雜品工業協會;承辦單位:北京匯捷通國際展覽有限公司'
b[b.index('承辦單位') + 5:]
2.字符串替換:
a='dddddddddd'
a.replace('要被替換的','替換的內容')
3.字符串比較
3.1 某個字符串內,是否包含某子字符串:
if '網址' in str(a):
print('字符串a中包含 網址 這兩個字')
3.2 字符串相等
if a==b:
print('a與b是相等的')
3.3 字符串數組比較,打印不同:
# 獲取整行和整列的值(列表)
print('開始比較:')
res = []
lista=['abcdef','hello','ok','buzhidao','defgh','women']
listb=['abcdef','hello','ok123','buzhidao','defgh']
for j in lista:
print(j)
if j in listb:
res.append(j)
print ('相同的字符串輸出:',res)
k=','.join(lista)
for j in res:
k=k.replace(str(j),'').replace(',','')
h = ','.join(listb)
for j in res:
h = h.replace(str(j), '').replace(',','')
#strc=k.replace(str(j))
print('a表格中獨有內容爲:',k)
print('b表格中獨有內容爲:',h)