Python 学习笔记(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_35212491/article/details/81534776

序列

字符串

字符串是一串字符的序列。'" 都可表示字符串,其工作机制完全相同,'''""" 可指定多行字符串。字符串是不可更改类型,字符串对象一旦被创建就不能被修改。

In [1]: 'Hello, Python!'
Out[1]: 'Hello, Python!'

In [2]: "Hello, Python!"
Out[2]: 'Hello, Python!'

In [3]: """Hello, 
   ...: Python!"""
Out[3]: 'Hello,\nPython!'

转义序列

转义序列如 \n 换行符,\t 水平制表符,\uhhhh unicode 16 位的十六进制值。

In [1]: 'Hello,\nPython!'
Out[1]: 'Hello,\nPython!'

In [2]: print('Hello,\nPython!')
Hello,
Python!

原始字符串

在引号前面加 rR 可以抑制字符转义,

In [3]: print(r"Hello,\nPython!")
Hello,\nPython!

字符串常用操作

help(str) 查看字符串基本操作

转换大小写
  • capitalize(self)
    将字符串首字母大写,其它字母变为小写
In [1]: var = 'xiaoXiao'

In [2]: var.capitalize()
Out[2]: 'Xiaoxiao'
  • title()
    将每个单词开头字母变为大写
In [1]: var = "xiao xiAO"

In [2]: var.title()
Out[2]: 'Xiao Xiao'
  • upper()
    将字符串全部转为大写字母
In [1]: var = "daming"

In [2]: var.upper()
Out[2]: 'DAMING'
  • lower()
    将字符串全部转为小写字母
In [1]: var ="DaMing"

In [2]: var.lower()
Out[2]: 'daming'
  • swapcase()
    将字符串大写字母变为小写,小写字母变为大写
In [1]: var ="DaMing"

In [2]: var.swapcase()
Out[2]: 'dAmING'
查找字符串
  • find(self, sub, start=None, end=None)
    查找某个字符串第一次出现的位置
In [1]: var = "Hello, Python! Hello, Java!"

In [2]: var.find("llo")     # 若存在,则返回第一次出现的位置
Out[2]: 2

In [3]: var.find("Ruby")    # 不存在,则返回-1
Out[3]: -1
  • index(self, sub, start=None, end=None)
    find() 功能相同,不同的是当查找的字符串不存在时会抛出异常
In [1]: var = "Hello, Python! Hello, Java!"

In [2]: var.index("llo")
Out[2]: 2

In [3]: var.index("Ruby")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-2aeceb75a96a> in <module>()
----> 1 var.index("Ruby")

ValueError: substring not found
  • rfind(self, sub, start=None, end=None)
    find,从字符串右侧开始查找
In [1]: var = "Hello, Python! Hello, Java!"

In [2]: var.rfind("llo")
Out[2]: 17
  • rindex(self, sub, start=None, end=None)
    index,从字符串右侧开始查找
In [1]: var = "Hello, Python! Hello, Java!"

In [2]: var.rindex("llo")
Out[2]: 17
  • count(self, sub, start=None, end=None)
    统计 sub 字符串出现的次数,可以指定统计的起始和结束的范围
In [1]: var = "hello, python! hello, c!"

In [2]: var.count("hello")
Out[2]: 2

In [3]: var.count("hello", 8)   # 从第8个字符开始统计
Out[3]: 1
  • startswith(self, prefix, start=None, end=None)
    判断字符串是否以某字符串开头
In [1]: var = "hello, python"

In [2]: var.startswith("hello")
Out[2]: True

In [3]: var.startswith("hi")
Out[3]: False
  • endswith(self, suffix, start=None, end=None)
    判断字符串是否是以某个字符串结尾
In [1]: var = "hello, python"

In [2]: var.endswith("python")
Out[2]: True

In [3]: var.endswith("java")
Out[3]: False
分割字符串
  • partition(self, sep)
    sep 为分隔符, 将字符串分割为3个元素的元组
In [1]: var = "abceabceabceabc"

In [2]: var.partition("e")
Out[2]: ('abc', 'e', 'abceabceabc')

In [3]: var.partition("d")
Out[3]: ('abceabceabceabc', '', '')
  • rpartition(self, sep)
    partition 一样,从字符串右侧开始查找分割
In [1]: var = "abceabceabceabc"

In [2]: var.rpartition("e")
Out[2]: ('abceabceabc', 'e', 'abc')
  • split(self, sep=None, maxsplit=None)
    sep 为分隔符,分割字符串返回列表,若 sep 未指定,则默认以空字符
In [1]: var = "abceabceabceabc"

In [2]: var.split("e")
Out[2]: ['abc', 'abc', 'abc', 'abc']

In [3]: var.split("e", 2)
Out[3]: ['abc', 'abc', 'abceabc']

In [4]: var.split("d")
Out[4]: ['abceabceabceabc']
  • rsplit(self, sep=None, maxsplit=None)
    split ,从字符串右侧为起始位置切割字符串
In [1]: var = "abceabceabceabc"

In [2]: var.rsplit("e", 1)
Out[2]: ['abceabceabc', 'abc']
  • splitlines(self, keepends=False)
    根据换行符 \n、\r\n、\r 分割字符串,若指定为True,则结果中保留换行符
In [1]: var = "abc\nabc\nabc\n"

In [2]: var.splitlines()
Out[2]: ['abc', 'abc', 'abc']

In [3]: var.splitlines(True)
Out[3]: ['abc\n', 'abc\n', 'abc\n']
字符串对齐
  • center(self, width, fillchar=None)
    将字符串居中,width 为总长度,fillchar 为填充的字符,默认为空格
In [1]: var = "mid"

In [2]: var.center(20)
Out[2]: '        mid         '

In [3]: var.center(20, "*")
Out[3]: '********mid*********'
  • ljust(self, width, fillchar=None)
    字符串左对齐,width 为长度, fillchar 为填充的字符,默认为空格
In [1]: var = "left"

In [2]: var.ljust(20)
Out[2]: 'left                '

In [3]: var.ljust(20, "#")
Out[3]: 'left################'
  • rjust(self, width, fillchar=None)
    字符串靠右对齐,width 为长度,fillchar 为填充的字符,默认填充空格
In [1]: var = "right"

In [2]: var.rjust(20, "*")
Out[2]: '***************right'
连接字符串
  • join(self, iterable)
    将一个序列连接成一个字符串
In [1]: "-".join(["a", "b", "c", "d"])
Out[1]: 'a-b-c-d'

In [2]: "#".join("abcd")
Out[2]: 'a#b#c#d'
字符串替换
  • replace(self, old, new, count=-1)
    新字符串替换老字符串
In [1]: var = "Hello, Python!"

In [2]: var.replace("Python", "World")
Out[2]: 'Hello, World!'
  • translate(self, table)
In [1]: tran = str.maketrans("abc", "123")

In [2]: var = "abccc"

In [3]: var.translate(tran)
Out[3]: '12333'
其他
  • strip(self, chars=None)
    删除首位空字符
  • lstrip(self, chars=None)
    删除左侧空字符
  • rstrip(self, chars=None)
    删除右侧空字符
In [1]: var = "\t\t\tHello\t\t\t"

In [2]: var
Out[2]: '\t\t\tHello\t\t\t'

In [3]: var.strip()
Out[3]: 'Hello'

In [4]: var.lstrip()
Out[4]: 'Hello\t\t\t'

In [5]: var.rstrip()
Out[5]: '\t\t\tHello'
  • isalnum(self)
    判断字符串是否是文字或者数字
  • isalpha(self)
    判断字符串是否是文字
  • isdigit(self)
    判断字符串是否是数字
  • islower(self)
    判断字符串是否是小写
  • isupper(self)
    判断字符串是否是大写
  • isspace(self)
    判断字符串是否全为空字符
  • ······

猜你喜欢

转载自blog.csdn.net/sinat_35212491/article/details/81534776
今日推荐