Python

Python—字符串(一)

2019-08-17  本文已影响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'
]

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

>>> a="hello world"
>>> a.capitalize()
'Hello world'
'hello world'
>>> a.count('o')
2
>>> a.count('o',6)
1
>>> a="hello world"
>>> a.encode('utf8')
b'hello world'
>>> a="hello world"
>>> a.startswith("he")
True
>>> a.endswith("d",0,5)
False
>>> 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
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'

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'

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

>>> "ddwa".isidentifier()
True
>>> "class".isidentifier()
True
>>> "1_d".isidentifier()
False
>>> "#$".isidentifier()
False
>>> b="HELLO WORLD 333"
>>> b.lower()
'hello world 333'
>>> b.casefold()
'hello world 333'
>>> a="HELLO world"
>>> a.islower()
False
>>> b="hello world 333"
>>> b.upper()
'HELLO WORLD 333'
>>> a="HELLO WORLD"
>>> a.isupper()
True
>>> b="hello\tworld25So#@$happy"
>>> b.title()
'Hello\tWorld25So#@$Happy'
>>> a="Hello\tWorld25So#@$Happy"
>>> a.istitle()
True
>>> a=["hello", "world"]
>>> '-'.join(a)
'hello-world'

# 当参数为一字典时,指挥拼接字典的键,可使用下面第二种方法拼接字典的值
>>> a={"hello":"a", "world":"b"}
>>> '-'.join(a)
'hello-world'
>>> '-'.join(a.values())
'a-b'
>>> a="ahello world\tand"
>>> a.strip('\tad')
'hello world\tan'
>>> a.lstrip('a')
'hello world\tand'
>>> a.rstrip('and')
'ahello world\t'
>>> a="hello we world"
>>> a.split(' ', maxsplit=1)
['hello', 'we world']
>>> a.rsplit(' ', maxsplit=1)
['hello we', 'world']
>>> a="hello world"
>>> a.replace('o','@')
'hell@ w@rld'
>>> a.replace('o','@',1)
'hell@ world'
上一篇 下一篇

猜你喜欢

热点阅读