phthon字符與list常用方法和屬性

字符

methods :

  1. count:統計字符在字符串中出現的次數(return int)

[searchStr,startIndex,endIndex]

mypty = 'This is a demo of the count method of str'
print(mypty.count('i', 0, len(mypty))) # 2
# 找不到則返回0
print(mypty.count('z', 0, len(mypty)))  # 0
  1. find 在字符串裏找到第一個符合查找條件的字符的下標(return int) 

[searchStr,startIndex,endIndex]

mypty = 'This is a demo of the find method of str'    
print(mypty.find('i', 0, len(mypty))) # 2  
# 找不到則返回-1
print(mypty.find('z', 0, len(mypty)))  # -1
  1. replace 在字符串中替換指定的字符 (return new str)

[beforeStr,afterStr,replaceCount] replaceCount:默認全部替換

mypty = 'This is a demo of the replace method of str'
newMypty = mypty.replace('is', 'are')
print(newMypty) # Thare are a demo of the replace method of str
  1. upper & isupper : 將字符轉換爲全大寫 & 字符內是否都是大寫

upper : (return upper str)
isupper : return Boolean

mypty = 'this is a demo of the upper method of str'
print(mypty.upper()) # THIS IS A DEMO OF THE UPPER METHOD OF STR
newMypty = mypty.upper()
print(newMypty.isupper()) # True
  1. lower & islower : 將字符轉換爲全小寫 & 字符內是否都是小寫

lower : (return lower str)
islower : return Boolean

mypty = 'THIS IS A DEMO OF THE LOWER METHOD OF STR'  
print(mypty.lower()) # this is a demo of the lower method of str
newMypty = mypty.lower()
print(newMypty.islower()) # True
  1. split : 將字符基於指定字符分割成一個list,沒有參數則默認以空格爲分割符

Return a list of the words in the string, using sep as the delimiter string.

[splitKey,splitCount]
return list

mypty = 'this is a demo of the split method of str'
print(mypty.split(' ')) # 基於空格分割 ['this', 'is', 'a', 'demo', 'of', 'the', 'split', 'method', 'of', 'str']

  1. strip : 去除一個字符前後兩邊的指定字符,不傳參則去除兩邊空格(一側有也可以替換,不穿參數則只能替換兩側空格)

如果想要替換中間的空格符,則可以使用str.replace(' ','')實現

Return a copy of the string with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.

return stripped str

# 沒有參數
mypty = '   this is a demo of the split method of str    '
print(mypty ) # '   this is a demo of the split method of str    ' # 初始有兩邊空格
print(mypty.strip()) # 'this is a demo of the split method of str' 無兩邊空格

#有參數
mypty = 'zzzthis is a demo of the split method of strzzz'
print(mypty) # 'zzzthis is a demo of the split method of strzzz'
print(mypty.strip('zzz')) # 'this is a demo of the split method of str'

#錯誤參數,則返回原字符
print(mypty.strip('sss')) # 'zzzthis is a demo of the split method of strzzz'

8.格式化輸出

  1. %s

語法:str%s%{value} 其中s指佔位符 ,後面的百分比後的大括號標識s對應的值

允許爲數字、字符、字l典、list類型

demostr = '致%s'%('愛麗絲')
print(demostr) # 致愛麗絲
demostr = '致%s'%(1>2)
print(demostr) # 致False
demostr = '致%s'%([1,2,3])
print(demostr) # 致[1, 2, 3]

  1. %d

語法: 只接收數字類型%d%(Number)

# 整型
numberStr = '只接受數字類型%d'%(269)
print(numberStr) # 只接受數字類型269
# 浮點型
doubleStr = '只接受數字類型且輸出爲整數(向下取整)%d'%(99.9)
print(doubleStr) # 只接受數字類型且輸出爲整數(向下取整)99

# 其他類型則會報錯
numberStr = '只接受數字類型%d'%('哈哈哈')
print(numberStr) # TypeError
  1. %f

語法:`只接受數字類型%f'%(Number) 其中整型會保留六位小鼠

floatStr = '只接受數字類型%f'%(269)
print(floatStr) # 只接受數字類型269.000000
#有小數的依然保留六位
floatStr = '只接受數字類型%f'%(269.9)
print(floatStr) # 只接受數字類型269.000000
#超過六位,看第七位,第七位大於5則第六位進1且捨去剩餘位數
## 進1
floatStr = '只接受數字類型%f'%(269.1111116)
print(floatStr) # 只接受數字類型269.111112
## 捨去
floatStr = '只接受數字類型%f'%(269.1111115)
print(floatStr) # 只接受數字類型269.111111  
  1. F/f 表達式
name = 19
mystr = F'我的名字是{name}'
print(mystr) # 我的名字是19

未完待續

以上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章