Python函數——.strip()

講述函數.strip()的使用方法。


博主在查閱此函數的使用方法時,未找到當.stip()參數爲空時其作用的例子,故自己測試了一次,現記錄下來:

現做個總結:.strip()參數爲空時,其作用是去除字符串首尾的所有空格。

  • 代碼:
a = 'abcd'      
b = 'abcd   '   # 後有3個空格
c = '  abcd   ' # 前有2個空格,後有3個空格

print('a:',len(a),  
      '\n b:',len(b),  
      '\n c:',len(c),
      
      '\n a.strip():',
      len(a.strip()), 
      '\n b.strip():',
      len(b.strip()),
      '\n c.strip():',
      len(c.strip() ))
  • 結果
a: 4 
b: 7 
c: 9 
a.strip(): 4 
b.strip(): 4 
c.strip(): 4
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章