字符串内置方法

一、字符串内置方法(str)

1.用途:描述性质的东西,如人的名字、单个爱好、地址、国家等
2.定义:使用''、""、''''''、""""""包裹的的一串字符

  • u'unicode': unicode编码的字符串
  • b'101': 二进制编码的字符串
  • r' ': 原生字符串,也就是说' '这是普通的两个字符,并没有换行的意思(一般用于路径的表示)

二、常用方法

1.1 核心方法
  1. 按索引取值
  2. 切片
  3. 长度len
  4. 成员运算in|not in
  5. 移除空白strip
  6. 切分split
  7. 循环

1.索引取值(只可取值,不可以改变)
eg:

s = "unicode encoding "
print(s[1])

结果:n

  1. 切片取值(顾头不顾尾)

    s = "unicode encoding "
    print(s[0:3]) # 顾头不顾尾
    print(s[0:4:1])  # 1表示从左到右
    
    print(s[-4::-1])  # -1表示从右到左  # 不推荐掌握
    
    print(s[4:0:-1])  # -1表示从右到左
    
    
   
结果:
   
   uni
   
   unic
   idocne edocinu
   ocin

3. for循坏

   eg:

   ``` python
   s = "code"
   for i in s:
       print(i)

结果:

c
o
d
e

  1. strip()

作用:移除空白或指定字符(删除两端),当strip参数不再字符串两端时,则不删除

eg1:

s = "   unicode encoding  "
print(s.strip())  # 去两端的空白

结果:

unicode encoding

eg2:

s = "*!****unicode encoding-***"
print(s.strip('-*!'))  # 指定多个字符一起去掉,只要strip里面有的字符就全部干掉
print(s.strip('unicode'))  # 指定多个字符一起去掉,只要strip里面有的字符就全部干掉
s1 = s.strip('*-!')  # 首先判断字符串s的两端字符,为*,再去strip里找有没有*,有就去掉,再去判断字符串s的两端字符,!-,再从strip里面找,有去掉,没有停止去掉
print(s1)

结果:

unicode encoding

  1. split

    作用:切割字符串,默认以空格切割字符串,可以指定切割次数

    eg:

    s = "*!****unicode encoding-***"
    print(s.split())  # 默认以空格切割字符串
    print(s.split('!'))  # 以!切割
    print(s.split('!', 2))
    

    结果:

    ['!unicode', 'encoding-']
    ['', 'unicode encoding-']
    ['', 'unicode encoding-']

  2. 成员运算in和not in

    eg:

    s = "*!****unicode encoding-***"
    print('*' in s)  # True
    print('$' not in s)  # 
    

    结果:

    True

    True

  3. 长度len()

    eg:

    s = "unicode encoding"
    print(len(s))
    

    结果:

    26

1.2 主要方法

  1. lstrip&rstrip,从左边开始&从右边开始

    eg:

    # 1. lstrip() 和 rstrip()
    s2 = "*!****unicode encoding-***"
    print(s2.lstrip('*'))
    print(s2.rstrip('*'))
    

    结果:

    !unicode encoding-
    !
    **unicode encoding-

  2. rsplit()从右边开始切割

    eg:

    # 2. rsplit()
    s2 = "*!****unicode encoding-***"
    print(s2.split('*', 1)) # 从左边开始,直接取一次
    print(s2.rsplit('*', 1)) # 从右边开始,直接取一次
    

    结果:

    ['', '!unicode encoding-']
    ['
    !****unicode encoding-**', '']

  3. lower&upper, 大写转小写&小写转换成大写

    eg:

    s2 = "unicode ENCODING"
    print(s2.lower())
    print(s2.upper())
    

    结果:

    unicode encoding
    UNICODE ENCODING

  4. startswith&endswith 判断以…开始&判断以…结束

    eg:

    s2 = "unicode ENCODING"
    print(s2.startswith('u'))
    print(s2.endswith('G'))
    

    结果:

    True

    True

  5. join(用的比较多)一般和split联用,并且使用连接字符串时候最好采用join方式,参数为容器

    eg:

    s3 = ' '
    print(s3.join(['234', '234', '234']))  # 以s3为间隔符,拼接列表里的每一个元素
    s = '辣条/薯片/汽水/泡面/火腿肠/枸杞/当归/鹿茸'
    
    s1 = s.split('/')
    print('*'.join(s1))
    

    结果:

    234 234 234

    辣条薯片汽水泡面火腿肠枸杞当归*鹿茸

  6. replace, 替换字符串中某个单个字母,其中参数可以指定替换的次数

    eg:

    s2 = "unicode ENCODING"
    print(s2.replace('o', 'abc'))
    

    结果:

    unicabcde ENCabcDING

  7. isdigit,判断字符串中是否只有数字(纯数字)

    eg:

    # str值isdigit()
    salary = '111'
    print(salary.isdigit())  # True
    
    salary = '111.1'
    print(salary.isdigit())  # False
    

    结果:

    True
    False

  8. isalpha(纯字母)

    s3 = 'aaac1c'
    print(s3.isalpha())
    

    结果:

    True

1.3 次要操作

  1. find|rfind|index|rindex|count

    eg:

    s2 = '**23423***ni234234ck $$ hand223423some******'
    print(s2.find('$'))  # 从左找,找到第一个停止,找不到返回-1
    print(s2.rfind('$'))  # 从右找,找到就停止,找不到返回-1
    print(s2.index('$'))  # 找不到报错
    print(s2.rindex('$'))  # 找不到报错
    print(s2.count("2"))  # 统计“2”,在字符串中出现的次数
    

    结果:21
    22
    21
    22
    7

  2. center|ljust|rjust|zfill

    s2 = 'unicode ENCODING'
    print(s2.center(50, '*'))  # 居中
    print(s2.ljust(50, '*'))  # 居左
    print(s2.rjust(50, '*'))  # 居右
    print(s2.zfill(50))  # 填充0居右
    

    结果:

    unicode ENCODING
    unicode ENCODING**********************************
    **********************************unicode ENCODING
    0000000000000000000000000000000000unicode ENCODING

  3. expandtabs, 针对 而言

    eg:

    s2 = 'a	a'
    print(s2)
    print(s2.expandtabs(8))  # 针对	而言
    

    结果:

    a a
    a a

  4. captalize|swapcase|title 只针对英文

    eg:

   s2 = 'harry Potter'
   
   print(s2.capitalize())  # 首字母(一句话的开头)大写,其他全小写,用在段落开始
   print(s2.swapcase())  # 大小写互换
   print(s2.title())  # 所有单词首字母大写

结果:

Harry potter
HARRY pOTTER
Harry Potter

1.4 is系列(只是为了告诉你,判断是否为数字时除了中文数字以后使用isdigit()即可)

  • isdecimal(): 检查字符串是否值包含十进制字符,如果是返回True,否则返回False。
  • isdigit(): 如果字符串只包含数字则返回True,否则返回False。
  • isnumeric(): 如果字符串中只包含数字字符,则返回True,否则返回False。
  • isalnum(): 如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False。
  • isalpha(): 如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False。
  • islower(): 如果字符串中只包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回True,否则返回False。
  • isspace(): 如果字符串中只包含空白,则返回True,否则返回False
  • isupper(): 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回True,否则返回False。
  • istitle(): 如果字符串是标题类型的(见title()),则返回True,否则返回False。

三、 存一个值还是多个值

存在一个值

四、 有序or无序

有序

五、 可变or不可变(重点)

	 * 可变:值变id不变,不可哈希
  • 不可变:值变id也变,可哈希

    不可变

eg:

s2 = 'abc'
print(id(s2))
s2 += 'abc'
print(id(s2))

结果:
2880872572216
2880875682256

六、总结

6.1常用

  1. 按索引取值
  2. 切片(顾头不顾尾,步长)
  3. 长度len
  4. 成员运算in|not in
  5. 移除空白strip
  6. 切分split
  7. 循环

6.2 常用方法

  1. lstrip&rstrip
  2. lower&upper
  3. startswith&endswith
  4. rsplit
  5. join
  6. replace
  7. isdigit
在当下的阶段,必将由程序员来主导,甚至比以往更甚。
原文地址:https://www.cnblogs.com/randysun/p/11290691.html