PYTHON機器學習自學/自修 整理[00004] ~ 語言技術:PYTHON GOSSIP(字串/字元處理)
PYTHON機器學習自學/自修 整理[00004] ~ 語言技術:PYTHON GOSSIP(字串/字元處理)
import sys
StrData="Justin"
print(StrData)#Justin
StrData='Justin'
print(StrData)#Justin
StrData="Just'in"
print(StrData)#Just'in
StrData='Just"in'
print(StrData)#Just"in
StrData='Just' 'in'
print(StrData)#Justin
StrData='Just''in'
print(StrData)#Justin
StrData='Just''in'
print(StrData)#Justin
StrData='c:\workspace'
print(StrData)#c:\workspace
StrData="c:\workspace"
print(StrData)#c:\workspace
StrData='c:\\workspace'
print(StrData)#c:\workspace
StrData="c:\\workspace"
print(StrData)#c:\workspace
StrData='c:/workspace'
print(StrData)#c:/workspace
StrData="c:/workspace"
print(StrData)#c:/workspace
StrData='c://workspace'
print(StrData)#c://workspace
StrData="c://workspace"
print(StrData)#c://workspace
StrData="Justin"
print(len(StrData))#計算字串長度
for c in StrData:
print(c, end='-')#依序取出字串字元[字串轉字元]
print('')
StrSub='Just'
blnAns=StrSub in StrData#字串尋找 比較
print(blnAns)
print(type(blnAns))
StrName01='jash'
StrName02='liao'
StrName=StrName01+StrName02#字串組合
print(StrName)
StrName_10=StrName*10#字串複製(重複10次)
print(StrName_10)
#只能複製不能縮短[unsupported operand type(s) for /: 'str' and 'int'] print(StrName_10/10)
intScore=90
StrScore="score:"+str(intScore);#數值轉字串
print(StrScore)
#[invalid literal for int() with base 10: 'score:90'] print(int(StrScore))
#[invalid literal for int() with base 10: 'score:90'] print(int('90分'))
print(int('90'))
chrData='廖'
print(ord(chrData))
print(chr(24278))
print(chrData.encode('big5'))
print(chrData.encode('utf-8'))
print(chrData.encode('big5').decode('big5'))
for i in range(0,len(StrName)-1):
print(StrName[i])#字串轉字元陣列
#字串 分割/切割/拆解
print(StrName[0:3])#jas[start:len]
print(StrName[3:])#hliao[start:]
print(StrName[:4])#jash[:len]
print(StrName[:-1])#jashlia
print(StrName[-5:-1])#hlia
print(StrName.index('i'))#第一個字元出現位置
print(StrName.upper())#轉大寫
print(StrName.lower())#轉小寫
print(StrName.startswith('J'))
print(StrName.startswith('j'))#判斷起始字元
#字串格式化輸出
text = '%d %.2f %s' % (1, 99.3, 'Justin')
print(text)
# %% 在字串 中顯示%
# %d 以10 進位整數方式輸出
# %f 將浮點 數以10進位方式輸出
# %e, %E 將浮點 數以10進位方式輸出,並使用科學記號
# %o 以8進 位整數方式輸出
# %x, %X 將整 數以16進位方式輸出
# %s 使用str()將字串輸出
# %c 以字元 方式輸出
# %r 使用repr()輸 出字串