9、split 和 format 函数

1、分割

Docstring:
S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
使用sep作为分隔符字符串,返回S中的单词列表。如果给定maxsplit,则最多完成maxsplit拆分。如果未指定sep或sep为None,则任何空白字符串都是分隔符,空字符串将从结果中删除。
Type:      builtin_function_or_method

  S.split(sep=None, maxsplit=-1) -> list of strings

  sep:指定分割符,分割符会从字符串中切掉

  maxsplit:分割次数。默认为 -1, 即分隔所有。

  • split(sep=None, maxsplit=-1) -> list of strings

  1. 从左至右
  2. sep 指定分割字符串,缺省的情况下空白字符串作为分隔符
  3. maxsplit 指定分割的次数,-1 表示遍历整个字符串
  4. 立即返回列表
  • S.rsplit(sep=None, maxsplit=-1) -> list of strings

  1.  从右向左开始切,但是输出的字符串字符不会反
  2. sep指定分割字符串,缺省的情况下空白字符串作为分隔符
  3. maxsplit 指定分割的次数,-1 表示遍历整个字符串
  4. 立即返回列表
  • S.splitlines([keepends]) -> list of strings
    Docstring:
    S.splitlines([keepends]) -> list of strings

    Return a list of the lines in S, breaking at line boundaries.
    Line breaks are not included in the resulting list unless keepends
    is given and true.
    Type:      builtin_function_or_method

  1. 按照行来切分字符串
  2. keepends 指的是是否保留分隔符
  3. 行分隔符包括 \n,\r\n,\r 等

 S.partition(sep) -> (head, sep, tail)

Docstring:
Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it.  If the separator is not
found, return S and two empty strings.
Type:      builtin_function_or_method

  1. 从左至右,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾 三部分的三元组
  2. 如果没有找到分隔符,就返回头、2个空元素的三元组
  3. sep 分割字符串,必须指定
rpartition(sep) -> (head, sep, tail)
从右至左,遇到分隔符就把字符串分割成两部分,返回头、分隔符、尾三部分的三元组
如果没有找到分隔符,就返回 2 个空元素和尾的三元组

2、替换

 S.replace(old, new[, count]) -> str

Docstring:
Return a copy of S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.
Type:      builtin_function_or_method
  1.  字符串中找到匹配替换为新子串,返回新字符串
  2. count 表示替换几次,不指定就是全部替换

3、移除

Docstring:
S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
Type:      builtin_function_or_method
  •  在字符串两端去除指定的字符集chars中的所有字符
  • 如果chars 没有指定,去除两端的空白字符
  • lstrip([chars]) -> str ,从左开始
    rstrip([chars]) -> str ,从右开始

 4、首尾判断

Docstring:
S.endswith(suffix[, start[, end]]) -> bool

Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
Type:      builtin_function_or_method
  •  S.endswith(suffix[, start[, end]]) -> bool

在指定的区间[start,end],字符串是否是suffix 结尾

  • startswith(prefix[, start[, end]]) -> bool
在指定的区间 [start, end) ,字符串是否是 prefix 开头

5、其它函数

upper() 大写
lower() 小写
swapcase() 交换大小写
isalnum() -> bool 是否是字母和数字组成
isalpha() 是否是字母
isdecimal() 是否只包含十进制数字
isdigit() 是否全部数字 (0~9)
isidentifier() 是不是字母和下划线开头,其他都是字母、数字、下划线
islower() 是否都是小写
isupper() 是否全部大写
isspace() 是否只包含空白字符
其他格式打印函数中文几乎不用,大家自行查看帮助

 6、format 函数

Python 2.5 之后,字符串类型提供了format 函数,功能更加强大,建议使用

"{} {xxx}".format(*args, **kwargs) -> str
  • args 是可变的位置参数
  • kwargs 是可变关键字参数,写作a=100
  • 使用花括号作为占位符
  • {} 表示按照顺序匹配位置参数,{n}表示取位置参数为n的值

  • {xxx}表示关键字参数中搜素名称一致
  • {{}}表示打印花括号

 7、字节序列

python3 引入了2个新的类型bytes、bytearray.

bytes不可变序列;bytearray是可变字节数组。

8、编码与解码

编码: str => bytes ,将字符串这个字符序列使用指定字符集 encode 编码为一个个字节组成的序列
bytes
解码: bytes bytearray => str ,将一个个字节按照某种指定的字符集解码为一个个字符串组成的
字符串

 

print("abc".encdoe())        # 缺省为utf-8编码
print("啊".encode('utf-8'))
print("啊".encode('gbk'))
print(b'abc'.decode('utf8'))
print(b'\xb0\xa1'.decode('gbk'))
123

 

 9、Bytes初始化

  • bytes() 空bytes
  • bytes(int) 指定字节的bytes ,被0填充
  • bytes(iterable_of_ints) -> bytes [0,255] 的int组成的可迭代对象
  • bytes(string, encoding[, errors]) -> bytes 等价于 string.encode()
  • bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer 从一个字节序列或者buffer复制出一个新的不可变的bytes对象
  • 使用 b 前缀定义
    只允许基本 ASCII 使用字符形式 b'abc9'
    使用 16 进制表示 b"\x41\x61"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值