• 【python基础】之str类字符串


    str类字符串是不可变对象

    1.创建字符串

    s1 = str() #创建一个空字符串
    s2 = str("hello") #创建字符串"hello" 

    2.处理字符串的常用函数和操作

    (1).函数

    len() 返回一个字符串的字符个数
    max() 返回字符串中最大的字符
    min() 返回字符串中最小的字符

    >>>s = "Welcome"
    >>>len(s)
    7
    >>>max(s)
    'o'
    >>>min(s)
    'W'

    字符串s为“welcome”,下面不再叙述

    (2).下标运算符[ ]

    s[ index ] 
    index 的范围为 [ 0, len(s) - 1].
    注意:python中允许负数最为下标
    >>>print(s[6], s[4])
    e o
    >>>print(s[-1], s[-3])
    e o

    (3).截取运算符 [start:end]

    [start:end] 截取并返回字符串s中下标从start开始到end-1结束的一个字串,若start > end,则返回空字符串
    >>>s[1:4]
    'elc'
    >>>s[1:-1] #也可以使用负数
    'elcom'
    >>>s[3:-8] #截取出现交叉返回空字符串
    ''
     

    (4).连接运算符 + ,复制运算符 *

    + :连接两个字符串。
    - :对字符串进行复制
    >>>s1 = "hello"
    >>>s2 = "world"
    >>>s1 + ' ' + s2
    'hello world'
    >>>3 * s1         #和s1 * 3 相同
    'hellohellohello'

    (5).判断一个字符串是否在另一个字符串中(in和not in)

    举例:s是字符串"Welcome"
    >>>'come' in s  #若为真,则返回ture
    True
    >>>'cat' in s   #若为假,则返回false
    False
    >>>'cat' not in s
    True

    (6).比较字符串(==, !=, >,<,,>=,<=)

    python通过字符串中的字符进行比较。
    从第一个字符开始比较。若第一个字符相同,则比较第二个字符,以此类推。若表达式成立,则返回True,否则返回False
    >>> s1 = 'integer'
    >>> s2 = 'int'
    >>> s1 == s2
    False
    >>> s1 < s2  # 'e' 的ARCII码值大于0,所以返回false
    False
    >>> s1 >= s2
    True
     

    (7).昳代字符串(用for循环)

    yi代字符串s:
    >>> for ch in s:
        print(ch)
    
        
    W
    e
    l
    c
    o
    m
    e
    >>> 

    3.其他字符串

    (1).测试字符串

    isalnum(): bool 如果这个字符串是字母数字且至少有一个字符,则返回true
    isalpha(): bool 如果这个字符串是字母且至少有一个字符,则返回true
    isdigit(): bool 如果这个字符串中只含有数字字符则返回true
    isdentifier(): bool    如果这个字符串是python标识符则返回true
    islower(): bool 如果字符串中所有的字符全是小写且至少有一个字符,则返回true
    isupper(): bool 如果字符串中所有的字符全是大写且至少有一个字符,则返回true
    isspace(): bool 如果字符串中所有的字符全是空格且至少有一个字符,则返回true


    (2).搜索字符串

    startswitch(s1: str): bool 若字符串是以子串是s1开始,则返回true
    endswitch(s1: str): bool 若字符串是以子串是s1结尾,则返回true
    find(s1): int 返回s1在字符串的最低下标,不存在则返回-1
    rfind(s1): int 返回s1在字符串的最高下标,不存在则返回-1
    count(sub string): int 返回子串在字符串中出现的无覆盖次数

    (3).转换字符串

    capitalize(): str 返回复制的字符串,并大写第一个字符
    lower(): str 返回复制的字符串,并将所有的字母转换为小写的
    upper(): str 返回复制的字符串,并将所有的字母转换为大写的
    title(): str 返回复制的字符串,并大写每个单词的首字母
    swapcase(): str 返回复制的字符串,并将大写字母转换为小写,小写字母转换为大写
    replace(old, new): str 返回新的字符串new,用new替换所有的旧字符串old出现的地方

    (4).删除字符串中的空格

    lstrip(): str 返回去掉前端空白字符串的子字符串
    rstrip(): str 返回去掉后端空白字符串的子字符串
    strip(): str 返回去掉两端空白字符串的子字符串

    (5).格式化字符串

    center(width): str 返回在给定宽度域上居中的字符串副本
    ljust(width): str 返回在给定宽度域上左对齐的字符串文本
    rjust(width): str 返回在给定宽度域上右对齐的字符串文本
    format(items): str  


     
  • 相关阅读:
    安装xcache3.0.3/3.2,为php加速
    编译安装lamp环境
    使用rpm包安装lamp环境
    centos6下通用二进制安装mysql5.5.33
    centos中进程管理工具
    centos6下的lvm逻辑卷的管理
    centos6中创建软raid方法
    解决vmware虚拟机克隆后启动centos报错device eth0 does not seem to be present, delaying initialization
    centos下常用文件管理命令
    centos下httpd-2.4的编译安装
  • 原文地址:https://www.cnblogs.com/libra-yong/p/6250254.html
Copyright © 2020-2023  润新知