vivion27 发表于 2018-8-14 10:39:47

【Python】07、python内置数据结构之字符串

In : s = "I love python"  

  
In : s.upper()
  
Out: 'I LOVE PYTHON'
  

  
In : s.lower()
  
Out: 'i love python'
  

  
In : s.title()      # 首字母全部大写
  
Out: 'I Love Python'
  

  
In : s.capitalize()   # 把首字母大写
  
Out: 'I love python'
  

  
In : print(s.center.__doc__)       # 在给定宽度下居中,可以使用单个字符填充
  
S.center(width[, fillchar]) -> str
  

  
Return S centered in a string of length width. Padding is
  
done using the specified fill character (default is a space)
  

  
In : s.center(50)
  
Out: '                  I love python                   '
  

  
In : s.center(50, "#")
  
Out: '##################I love python###################'
  

  
In : s.center(50, "#%")
  
---------------------------------------------------------------------------
  
TypeError                                 Traceback (most recent call last)
  
<ipython-input-13-4aa39ce1c3b3> in <module>()
  
----> 1 s.center(50, "#%")
  

  
TypeError: The fill character must be exactly one character long
  

  
In : s
  
Out: 'I love python'
  

  
In : s.zfill(5)
  
Out: 'I love python'
  

  
In : s.zfill(50)      # 用0填充
  
Out: '0000000000000000000000000000000000000I love python'
  

  
In : print(s.casefold.__doc__)
  
S.casefold() -> str
  

  
Return a version of S suitable for caseless comparisons.
  

  
In : s
  
Out: 'I love python'
  

  
In : s.casefold()   # 返回一个统一大小写的str,在不同平台有不同的表现形式
  
Out: 'i love python'
  

  
In : s.swapcase()   # 交换大小写
  
Out: 'i LOVE PYTHON'
  

  
In : "\t".expandtabs()# 默认将\t转换为8个空格
  
Out: '      '
  

  
In : "\t".expandtabs(8)
  
Out: '      '
  

  
In : "\t".expandtabs(3)
  
Out: '   '
页: [1]
查看完整版本: 【Python】07、python内置数据结构之字符串