字符串

在字符串的前面加上 'r',即可去掉字符串 ' '," " 之间如@,/,|的意义

如果需要加入 'u'使字符串变为unicode形式,需要把'u' 放在 'r' 之前

username = '  hhh**  '

## strip,lstrip,rstrip

用法:

  去除左侧空格:  username.lstrip(' ')

  去除所有'*': username.strip('*')

  去除右侧空格: username.rstrip(' ')

## lower,upper

用法:

  所有字体小写:username.lower()

  所有字体大写:username.upper()

## startwith,endwith

用法:

  判断是否以('al')开头:username.startwith('al')

  判断是否以('x')结尾: username.endwith('x')

## format

用法:

  1. kk =  ' { }  { }  { } ' .format('18','kk','male')

  2. kk = ' {0}  {1}  {0}'.format('18','kk','male')    ## 前面的 '0' '1' 当前面{ } 中的数字为’0‘时 把后面括号内 索引相对的字符 填进前面{ }中

  3. kk = '{username}  {age}  {sex}' .format(username ='kk',sex = 'male',age = 18)     与后面排列顺序无关,找对应值填入

## split,rsplit

  username = 'hello world fuck you'

  print(username.split())  == ['hello','world','fuck','you']

  username = 'root:h:h:/n/n/n'

  username.split(':')    #指定split后括号内的符号为分隔符

  username = 'c:/a/b/c/d.txt'

  username.split('/',1)   #指定 '/'为分隔符且只切割从左侧数第一个(切割几个由数字决定)

  rsplit 的用法与 split相同 仅从右侧开切

## join

  tag  = ' '

  tag.join(['egon','say','hello','world'])  #在所有字符之间加入 tag所被赋予的符号 如 '—','空格'

                  #此处join之所以加入  '[]' 是因为join后空格内只能加入一个量 

## replace

  username = 'alex say : i have one tesla , my name is alex'

  username.replace('alex','sb',1)    #加入1 代表仅替换第一个 'alex' 如不加入数字即替换所有'alex'


  

原文地址:https://www.cnblogs.com/christmassa/p/8995350.html