zoukankan      html  css  js  c++  java
  • python学习笔记(九)之字符串

    定义字符串

    1 >>> mystring = 'Hello Python'
    2 >>> name = str('Mountain')
    3 >>> mystring
    4 'Hello Python'
    5 >>> name
    6 'Mountain'
    View Code

    通过索引或字符串切片可以访问字符串

     1 >>> for i in range(len(name)):
     2 ...     print(name[i])
     3 ... 
     4 M
     5 o
     6 u
     7 n
     8 t
     9 a
    10 i
    11 n
    12 >>> mystring[:5]
    13 'Hello'
    View Code

    字符串也适用比较操作符,逻辑操作符和成员操作符。

    1 >>> mystring > name
    2 False
    3 >>> not mystring > name
    4 True
    5 >>> 'm' in name
    6 False
    7 >>> 'M' in name
    8 True
    View Code

    字符串一些方法

    capitalize() 把字符串中的首字符大写
    casefold() 把字符串所有字符改为小写
    center(width)  将字符串居中,并使用空格填充长度width的新字符串
     count(sub[,start[,end]])  返回sub在字符串里边出现次数,start,end表示范围
     encode(encoding = 'utf-8',errors = 'strict')   以encoding指定的格式对字符串进行编码
    endswith(sub[,start[,end]]) 检查字符串是否以sub结尾
    expandtabs([tabsize = 8]) 把字符串中tab符号( )转换为空格,如果不指定参数,默认空格数是tabsize = 8
    find(sub[,start[,end]]) 检测sub是否包含在字符串中,有返回索引,否则返回-1
    index(sub[,start[,end]]) 和find方法一样,如果find不在字符串中会产生一个异常
    strip(chars) 删除前后空格,chars参数定制删除的字符
    isalnum() 如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False
    isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False
     isdecimal() 如果字符串只包含十进制数字则返回True,否则False
    isdigit() 如果字符串只包含数字则返回True,否则返回False
    islower() 如果字符串中至少包含一个区分大小写的字符,且这些字符为小写,则返回True,否则False
    isnumeric() 是否只包含数字字符
    isspace() 是否只包含空格
    istitle() 字符串是否是标题化的
    isupper() 和islower()相反
    join(sub) 以字符串为分隔符,插入到sub中所有字符之间
    ljust(width) 返回一个左对齐字符串,并使用空格填充至长度为width的新字符串
    lstrip(chars) 删除左侧字符
    lower() 转换字符串中所有大写字符为小写
    partition(sub) 找到子字符串sub,把字符串分为一个元组(pre-sub,sub,fol-sub),如果不包含sub,返回(源字符串,‘ ’,‘ ’)
    replace(old,new[, count]) 把字符串中的子字符串替换成new子字符串,如果count指定,则替换次数不超过count次
    rfind(sub[,start[,end]]) 类似于find,从右开始找
    rindex(sub[,start[,end]]) 类似于index,从右开始找
    rjust(width) 类似于ljust,右对齐
    rpartition(sub) 类似于partition(),从右开始找
    rstrip() 删除右侧字符
    split(sep = None, maxsplit = -1) 不带参数默认是以空格为分隔符切片字符串,如果maxsplit参数设置,则仅分割maxsplit个子字符串,返回切片后的字符串拼接的列表
    splitlines(([keepends])) 按照' '分割,返回一个包含各行作为元素的列表,如果keepends参数指定,则返回前keepends行
    startswith(prefix[,start[,end]]) 是否以prefix开头
    swapcase() 转换大小写
    title() 返回标题化的字符串
    translate(table) 根据table的规则(可由str.maketrans('a', 'b')定制)转换字符串中的字符
    upper() 转换字符串中的所有小写字符为大写
    zfill(width)

    返回长度为width的字符串,右对齐,前面用0填充

     

      examples:

     1 >>> temp = 'hello python'
     2 >>> temp.capitalize()
     3 'Hello python'
     4 >>> temp.capitalize().casefold()
     5 'hello python'
     6 >>> temp.center(20)
     7 ' hello python '
     8 >>> temp.count('o', 0, len(temp))
     9 2
    10 >>> temp.encode()
    11 b'hello python'
    12 >>> temp = 'hello	python'
    13 >>> temp
    14 'hello	python'
    15 >>> temp.expandtabs()
    16 'hello python'
    17 >>> temp.expandtabs(16)
    18 'hello python'
    19 
    20 >>> temp
    21 'hello	python'
    22 >>> temp.find('o')
    23 4
    24 >>> temp.index('o')
    25 4
    26 >>> temp.strip('hn')
    27 'ello	pytho'
    28 
    29 >>> temp.join(' ')
    30 
    31 ' hello	python hello	python hello	python hello	python '
    32 >>> temp.join('abcd')
    33 'ahello	pythonbhello	pythonchello	pythond'
    34 >>> temp.ljust(20)
    35 'hello	python '
    36 >>> temp.rjust(20)
    37 ' hello	python'
    38 >>> temp.capitalize()
    39 'Hello	python'
    40 >>> temp.capitalize().lower()
    41 'hello	python'
    42 >>> temp.capitalize().upper()
    43 'HELLO	PYTHON'
    44 >>> temp.partition('	')
    45 ('hello', '	', 'python')
    46 >>> temp.split('	')
    47 ['hello', 'python']
    48 >>> test = 'abbbbbbbbbc'
    49 >>> test.translate(str.maketrans('b','t'))
    50 'atttttttttc'
    View Code
  • 相关阅读:
    OCP 062【中文】考试题库(cuug内部资料)第19题
    OCP 062【中文】考试题库(cuug内部资料)第18题
    OCP 062【中文】考试题库(cuug内部资料)第17题
    743. 网络延迟时间 力扣(中等) 最短路径SPFA,不熟练
    1337. 矩阵中战斗力最弱的 K 行 力扣(简单) 确实简单,结构体排序,二分也可
    171. Excel 表列序号 力扣(简单) 想不明白的题
    987. 二叉树的垂序遍历 力扣(困难) bfs+hash+优先队列 感觉还是简单的,就是复杂了点
    46. 全排列 力扣(中等) 容器或回溯
    1947. 最大兼容性评分和 周赛 力扣(中等) 排列next_permutation用法
    1104. 二叉树寻路 力扣(中等) 数学题,思考久了
  • 原文地址:https://www.cnblogs.com/ZGreMount/p/7761331.html
Copyright © 2011-2022 走看看