美文网首页Python
Python—字符串(一)

Python—字符串(一)

作者: 八戒无戒 | 来源:发表于2019-08-17 22:57 被阅读0次

python的字符串类型有很多方法,可以看到如下,本篇仅记录常用的一些方法:

[
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 
'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 
'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 
'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'
]

字符串为不可变类型,因此他的所有方法都不会改变自身的值,改变的仅是返回值

  • capitalize(): 字符串首字母变大写,并返回新字符串
>>> a="hello world"
>>> a.capitalize()
'Hello world'
  • count(sub, [start, end]):查找sub字符在字符串中出现的次数,start指定查找开始的索引位置,end指定结束的索引位置,并返回一个整数
'hello world'
>>> a.count('o')
2
>>> a.count('o',6)
1
  • encode([encoding, errors]):以指定的编码编译字符串,并返回一个编译后字节字符串,encoding默认位'utf8',errors为指定的错误处理方案,默认“strict”,可设置为 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace'
>>> a="hello world"
>>> a.encode('utf8')
b'hello world'
  • endswith(suffix, [start, end]): 判断字符串是否已suffix结尾,start指定开始位置,end指定结束位置,返回布尔值

  • startswith(suffix, [start, end]): 判断字符串是否已suffix结尾,start指定开始位置,end指定结束位置,返回布尔值

>>> a="hello world"
>>> a.startswith("he")
True
>>> a.endswith("d",0,5)
False
  • find(sub, start, enrd): 在字符串中从左边开始查找sub字符的位置(仅返回第一个查找到)的索引,start和end分别指定查找的开始和结束位置,返回最左(最小)边索引,未找到返回-1

  • rfind(sub, start, end): 在字符串中从右边开始查找sub字符的位置(仅返回第一个查找到)的索引,start和end分别指定查找的开始和结束位置,返回最右边(最大)索引,未找到返回-1

  • index(sub, start, end): 在字符串中从左边开始查找sub字符的位置(仅返回第一个查找到)的索引,start和end分别指定查找的开始和结束位置,返回最左(最小)边索引,未找到抛出异常

  • rindex(sub, start, end): 在字符串中从右边开始查找sub字符的位置(仅返回第一个查找到)的索引,start和end分别指定查找的开始和结束位置,返回最右边(最大)索引,未找到抛出异常

>>> a="hello we world"
>>> a.find('w')
6
>>> a.rfind('w')
9
>>> a.index('w')
6
>>> a.rindex('w')
9
>>> a.rfind('z')
-1
>>> a.index('z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
  • format():格式化输出,有三种用法,但第三种用法不建议采用。
In [1]: a="{} {}"                                                                                                                                                                                                
In [2]: a.format('hello','world')                                                                                                                                                                                
Out[2]: 'hello world'

In [3]: b="{a} {b}"                                                                                                                                                                                              
In [4]: b.format(a="hello", b="world")                                                                                                                                                                           
Out[4]: 'hello world'

In [7]: a="{2}  {0}  {1}"                                                                                                                                                                                          
In [8]: a.format("world", "coding", "hello")                                                                                                                                                                     
Out[8]: 'hello world coding'

  • format_map(mapping): 格式化输出,不常用,参数必须是一个字典,格式化占位符"{}"必须是字典的键,用法如下:
In [9]: a={"a":"hello", "b":"world", "c":{"test":"this is a dict"}}                                                                                                                                              
In [10]: "{a}  {b} {c[test]}".format_map(a)                                                                                                                                                                       
Out[10]: 'hello world this is a dict'

  • isalnum(): 判断字符串是否只包含数字或字母(不包括空格和特殊字符),返回一个布尔值

  • isalpha(): 判断字符串是否只包含字母(不包括空格和特殊字符),返回一个布尔值

  • isascii(): 判断字符串是否只包含ascii码,返回一个布尔值,3.7版本新增

  • isdecimal(): 判断字符串是否只包含十进制数字字符,返回一个布尔值

  • isdigit(): 判断字符串是否只包含数字字符,返回一个布尔值

  • isnumeric(): 判断字符串是否只包含数值字符,返回一个布尔值

isdecimal、isdigit、isnumeric三者区别:
>>> a="123"                # unicode数字时
>>> a.isdigit()
True
>>> a.isdecimal()
True
>>> a.isnumeric()
True


>>> a=b"123"                # 字节数字时
>>> a.isdigit()
True
>>> a.isdecimal()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'isdecimal'
>>> a.isnumeric()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'isnumeric'


>>> a="六"                #汉字小写数字时
>>> a.isdigit()
False
>>> a.isdecimal()
False
>>> a.isnumeric()
True


>>> a="Ⅳ"                #罗马数字时
>>> a.isdigit()
False
>>> a.isdecimal()
False
>>> a.isnumeric()
True


>>> a="肆"                #汉字大写数字时
>>> a.isdigit()
False
>>> a.isdecimal()
False
>>> a.isnumeric()
True

  • isidentifier(): 根据语言定义判断是否一个有效的标识符,返回一个布尔值
>>> "ddwa".isidentifier()
True
>>> "class".isidentifier()
True
>>> "1_d".isidentifier()
False
>>> "#$".isidentifier()
False
  • isprintable(): 判断是否为一个可打印字符串,返回一个布尔值

  • isspacer(): 判断字符串是否为空字符串,返回一个布尔值

  • casefold(): 字符串所有大写变小写(对任意语言有效),并返回新字符串

  • lower(): 字符串所有大写变小写(只对ascii编码字符),并返回新字符串

  • islower(): 判断字符串是否全部小写,返回一个布尔值

>>> b="HELLO WORLD 333"
>>> b.lower()
'hello world 333'
>>> b.casefold()
'hello world 333'
>>> a="HELLO world"
>>> a.islower()
False
  • upper(): 字符串中小写字母全部变为大写,返回一个新字符串

  • isupper(): 判断字符串是否全部大写,返回一个布尔值

>>> b="hello world 333"
>>> b.upper()
'HELLO WORLD 333'
>>> a="HELLO WORLD"
>>> a.isupper()
True
  • title(): 字符串中被空格、数字、符号等隔开的首字母b变大写,返回一个新字符串

  • istitle(): 判断字符串中被空格、数字、符号等隔开的首字母是否大写,返回一个布尔值

>>> b="hello\tworld25So#@$happy"
>>> b.title()
'Hello\tWorld25So#@$Happy'
>>> a="Hello\tWorld25So#@$Happy"
>>> a.istitle()
True
  • join(iterable): 将迭代对象中的元素进行拼接,参数可以是元祖,列表或者字典等。用法如下:
>>> a=["hello", "world"]
>>> '-'.join(a)
'hello-world'

# 当参数为一字典时,指挥拼接字典的键,可使用下面第二种方法拼接字典的值
>>> a={"hello":"a", "world":"b"}
>>> '-'.join(a)
'hello-world'
>>> '-'.join(a.values())
'a-b'
  • strip(chars): 字符串去除首尾的chars中出现的字符(若不传,默认去除空格),并返回一个新字符串

  • lstrip(chars): 字符串去除开头的chars中出现的字符(若不传,默认去除空格),并返回一个新字符r串

  • rstrip(chars): 字符串去除结尾的chars中出现的字符(若不传,默认去除空格),并返回一个新字符串

>>> a="ahello world\tand"
>>> a.strip('\tad')
'hello world\tan'
>>> a.lstrip('a')
'hello world\tand'
>>> a.rstrip('and')
'ahello world\t'
  • split(seq, maxsplit): 返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串,从最右边开始拆分。 如果给出了 maxsplit,则最多进行 maxsplit 次拆分(因此,列表最多会有 maxsplit+1 个元素)。 如果 maxsplit 未指定或为 -1,则不限制拆分次数(进行所有可能的拆分)

  • rsplit(seq, maxsplit): 返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串, 从最左边开始拆分。 如果给出了 maxsplit,则最多进行 maxsplit 次拆分(因此,列表最多会有 maxsplit+1 个元素)。 如果 maxsplit 未指定或为 -1,则不限制拆分次数(进行所有可能的拆分)

>>> a="hello we world"
>>> a.split(' ', maxsplit=1)
['hello', 'we world']
>>> a.rsplit(' ', maxsplit=1)
['hello we', 'world']
  • replace(old, new, [count]): 将原字符串中的old字符替换为new字符,并返回一个新字符串。count指定替换的次数,默认替换所有。
>>> a="hello world"
>>> a.replace('o','@')
'hell@ w@rld'
>>> a.replace('o','@',1)
'hell@ world'

相关文章

  • python基础知识(3)

    python字符串 python转义字符 python字符串运算符 python字符串格式化 python格式化操...

  • python count()方法详解

    Python count()方法 Python 字符串 描述 Python count() 方法用于统计字符串里某...

  • python字符串格式化符号与内建函数资料表

    python字符串格式化符号: Python 的字符串内建函数 Python 的字符串常用内建函数如下:

  • python字符串

    总结一下python字符串,方面自己查阅和复习。 字符串型:Python序列类型,本质:字符串序列 字符串的创建:...

  • 字符串

    一、Python 2 和Python 3 最大的区别就是字符串 Python 2 中的字符串是byte的有序序列 ...

  • Python(一)字符串操作

    Python字符串的操作 一: python字符串的格式 双引号或者单引号中的数据都是字符串 二: 字符串的输出 ...

  • 字符串操作方法

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • python字符串相关函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-09-28自学习资料

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 字符串内置函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

网友评论

    本文标题:Python—字符串(一)

    本文链接:https://www.haomeiwen.com/subject/dozcsctx.html