由單引號、雙引號、及三層引號括起來的字符python
str = 'hello,sheen' str = "hello,sheen" str = """hello,sheen""" #三層引號可輸出內容的特定格式
一個反斜線加一個單一字符能夠表示一個特殊字符,一般是不可打印的字符git
/t = 'tab',/n = '換行',/" = '雙引號自己'
| %d | 整數 |
| %f | 浮點數 |
| %s | 字符串 |
| %x | 十六進制整數 |spa
>>> h[0] #正向索引從0開始 'h' >>> h[-1] #反向索引從-1開始 'o'
s[start:end:step] # 從start開始到end-1結束, 步長爲step; - 若是start省略, 則從頭開始切片; - 若是end省略, 一直切片到字符串最後; s[1:] s[:-1] s[::-1] # 對於字符串進行反轉 s[:] # 對於字符串拷貝
in | not in
>>> 'o' in s True >>> 'a' in s False >>> 'a' not in s True
a = 'hello' b='sheenstar' print("%s %s" %(a,b)) hello sheenstar a+b 'hellosheenstar' a+' '+b 'hello sheenstar'
print('*'*20+a+' '+b+"*"*20) ********************hello sheenstar********************
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper'
'lower', 'upper', 'title'code
'Hello'.istitle() #判斷是不是標題 True '780abc'.isalnum() #判斷是不是數字或字母 True '780'.isdigit() #判斷是不是數字 True 'abd'.isalpha() #判斷是不是字母 True 'abd'.upper() #轉換爲大寫 'ABD' 'ADE'.lower() #轉換爲小寫 'ade' 'sheenSTAR'.swapcase() 'SHEENstar'
endswith
startswithblog
name = "yum.repo" if name.endswith('repo'): print(name) else: print("error") yum.repo
strip
lstrip
rstrip
注意: 去除左右兩邊的空格, 空格爲廣義的空格, 包括: n, t, r索引
>h = ' hello ' >h.strip() 'hello' >h ' hello ' >h.rstrip() ' hello' >h ' hello ' >h.lstrip() 'hello '
find:搜索
replace:替換
count:出現次數圖片
>>> h = "hello sheen .hello python" >>> h.find("sheen") 6 >>> h.rfind("sheen") #從右側開始找,輸出仍爲正向索引值 6 >>> h.rfind("python") 19 >>> h.replace("sheen",'star') 'hello star .hello python' >>> h 'hello sheen .hello python' >>> h.count('hello') 2 >>> h.count('e') 4
split:分離
join:拼接ip
>>> date = "2018/08/11" >>> date.split('/') ['2018', '08', '11'] >>> type(date.split('/')) <class 'list'> >>>list=["1","3","5","7"] >>>"+".join(list) '1+3+5+7'