zoukankan      html  css  js  c++  java
  • Python基础-字符串

    字符串的创建

    s = "Hello World"

    字符串常用操作

    join()  将序列中的元素以指定的字符连接生成一个新的字符串

    >>> s = ["1", "2", "3", "4"]
    >>> ".".join(s)
    1.2.3.4

    strip()  移除字符串头尾指定的字符(默认为空格)

    >>> s = " Hello World "
    >>> s.strip()
    Hello World
    >>> s = "*Hello World&"
    >>> s.strip("*")
    Hello World&

    lstrip()  截掉字符串左边的空格或指定字符

    >>> s = "*Hello World*"
    >>> s.lstrip("*")
    Hello World*

    index(str, beg=0, end=len(string))  检测字符串中是否包含子字符串 str,不存在则报错(beg:开始位置,默认为0;end:结束位置,默认为字符串字最后一个位置)

    >>> s = "Hello World"
    >>> s.index("e")
    1
    >>> s.index("z")
    ValueError:substring not found

    rindex()  返回字符串最后一次出现的位置,如果没有则报错

    >>> s = "Hello World"
    >>> s.rindex("o")
    7

    find()  检测字符串中是否包含子字符串 str,不存在返回-1

    >>> s = "Hello World"
    >>> s.find("e")
    1
    >>> s.find("z")
    -1

    rfind()  返回字符串最后一次出现的位置,如果没有匹配项则返回-1

    split(str="", num=string.count(str))  通过指定分隔符对字符串进行切片(str:指定字符串,默认为所有空字符;num:分隔次数),返回列表

    >>> s = "www.luffycity.com"
    >>> s.split(".")
    ['www', 'luffycity', 'com']

    splitlines()  按照行(' ', ' ', ')分隔,返回一个包含各行作为元素的列表

    >>> s = """luffycity
    namei"""
    >>> s.splitlines()
    ['luffycity', 'namei']

    count(sub, start= 0,end=len(string))  统计字符串里某个字符出现的次数(sub:统计的字符串)

    >>> s = "Hello World"
    >>> s.count("o", 2, 9)
    2

    replace(old, new[, max])  将字符串中的子字符串替换(old:旧字符串;new:新字符串;max:替换最大次数)

    >>> s = "Hellz Wzrld"
    >>> s.replace("z", "o")
    Hello World

    endswith()  是否以指定后缀结尾

    >>> s = "Hello World"
    >>> s.endswith("d")
    True
    >>> s.endswith("l")
    False

    startswith()  是否以指定字符串开头

    >>> s = "Hello World"
    >>> s.startswith("H")
    True
    >>> s.startswith("e")
    False

    isdigit()  是否为数字

    >>> s = "12345"
    >>> s.isdigit()
    True
    >>> s = "asdfg"
    >>> s.isdigit()
    False

    isalnum()  是否由字母和数字组成

    isalpha()  是否只由字母组成

    isdecimal()  是否只包含十进制字符

    isidentifier()  是否包含该语言的保留字

    islower()  是否由小写字母组成

    isprintable()  是否可打印

    isspace()  是否只由空白字符组成

    istitle()  单词拼写首字母是否为大写,且其他字母为小写

    title()  方法返回"标题化"的字符串,所有单词都是以大写开始,其余字母均为小写

    >>> s = "hello world"
    >>> s.title()
    Hello World

    isupper()  所有的字母是否都为大写

    upper()  将小写字母转换成大写字母

    >>> s = "Hello World"
    >>> s.upper()
    HELLO WORLD

    capitalize()  将第一个字母大写,其他字母小写

    1 >>> s='hello world'
    2 >>> s.capitalize()
    3  'Hello world'

    casefold()  将大写字母转换成小写字母

    >>> s='Hello World'
    >>> s.casefold()
     'hello world'

    lower()  字符串中所有大写字符为小写,只适用于‘A~Z’

    swapcase()  大小写字母进行转换

    >>> s = "Hello World"
    >>> s.swapcase()
    hELLO wORLD

    center(width[, fillchar]) 如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充(width:指定宽度;fillchar:填充字符)

    >>> s = "Hello World"
    >>> s.center(25, "*")
    *******Hello World*******       

    ljust(width[, fillchar])  返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串(width:指定宽度;fillchar:填充字符)

    >>> s = "Hello World"
    >>> s.ljust(25, "*")
    Hello World**************

    rjust(width[, fillchar])  返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串

    >>> s = "Hello World"
    >>> s.ljust(25, "*")
    **************Hello World

    edcode()  以指定的编码格式编码字符串

    >>> s = "Hello World"
    >>> s.encode(encoding="utf-8")
    b'Hello World

    expandtabs()  把字符串中的 tab 符号(' ')转为空格,默认为8

    >>> s = "	 Hello World 	"
    >>> s.expandtabs(16)
                     Hello World    

    partition()  返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串

    >>> s = "Hello.World"
    >>> s.partition(".")
    ('Hello', '.', 'World')

    zfill()  返回指定长度的字符串,原字符串右对齐,前面填充0

    >>> s = "Hello World"
    >>> s.zfill(25)
    00000000000000Hello World
  • 相关阅读:
    SQLSERVER查询所有数据库名,表名,和字段名
    SQL通过拆分某字段中的内容来实现与对应表连接查询
    [SPOJ]CIRU 圆并
    有关反演和GCD
    docker部署 jenkins
    mongoDB学习记录(二)
    docker动态修改容器限制
    ORACLE数据库误操作DELETE并且提交数据库之后如何恢复被删除的数据
    用8个命令调试Kubernetes集群
    db2服务器linux的cache过高原因
  • 原文地址:https://www.cnblogs.com/ExBurner/p/8459038.html
Copyright © 2011-2022 走看看