首页 > 代码库 > Python基础2 之2

Python基础2 之2

参考地址:https://www.cnblogs.com/alex3714/articles/5717620.html

字符串操作

>>> line = "my name is liaowei ."
#首字母大写
>>> line.capitalize()
My name is liaowei .
>>> line
#大写全部变小写
>>> line.casefold()
my name is liaowei .

#局中显示
>>> line.center(40," ")
          my name is liaowei .          
>>> line.center(40,"-")
----------my name is liaowei .----------
#计算子串的个数
>>> line.count("i")
3
#转为byte类型
>>> line.encode()
bmy name is liaowei .
#判断一什么结尾
>>> line.endswith(".")
True
>>> line.endswith("  ")
False
#将\t转为多少个空格
>>> "liaowei\tshihaoren"
liaowei\tshihaoren
>>> "liaowei\tshihaoren".expandtabs(10)
liaowei   shihaoren
>>> "liaowei\tshihaoren".expandtabs(20)
liaowei             shihaoren
>>> "liaowei\tshihaoren".expandtabs(1)
liaowei shihaoren
#查找子串
>>> line.find("A")
-1
>>> line.find("i")
8

#返回子串的索引
>>> line
my name is liaowei .
>>> line.index("i")
8
#format
>>> "__1.{}___2.{}__".format("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> "__1.{}___2.{}__".format("a",2)
__1.a___2.2__
>>> "__1.{0}___2.{0}__".format("a",2)
__1.a___2.a__
>>> "__1.{}___2.{}__".format("a",2, 3)
__1.a___2.2__
>>> "__1.{one}___2.{two}__".format("a",2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: one
>>> "__1.{one}___2.{two}__".format(one="a",two=2)
__1.a___2.2__
>>> "__1.{one}___2.{two}__".format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: one
>>> "__1.{one}___2.{two}__".format(one="a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: two
>>> "__1.{one}___2.{two}__".format_map("one":"a","two":2)
  File "<stdin>", line 1
    "__1.{one}___2.{two}__".format_map("one":"a","two":2)
                                            ^
SyntaxError: invalid syntax
>>> "__1.{one}___2.{two}__".format_map({"one":"a","two":2})
__1.a___2.2__
>>> "123".isdigit()
True
>>> "123a".isdigit()
False
>>> "123a".isnumeric()
False
>>> "123".isnumeric()
True
>>> "123.01".isnumeric()
False
>>> "123".isspace()
False
>>> "  ".isspace()
True
>>> "    \n\t\r ".isspace()
True
>>> "My name is".istitle()
False
>>> "My Name Is".istitle()
True
>>> "My Name Is".isupper()
False
>>> "ABC".isupper()
True
>>> "ABC ".isupper()
True
>>> "ABC 123".isupper()
True
>>> "ABC 123a".isupper()
False
#join
>>> "!".join([alex,jack,rain])
alex!jack!rain
>>> "".join([alex,jack,rain])
alexjackrain
>>> "@@@".join([alex,jack,rain])
alex@@@jack@@@rain

#maketrans
>>> intab = "aeiou"
>>> outtab = "12345" 
>>> trantab = str.maketrans(intab, outtab)
>>> str = "this is string example....wow!!!"
>>> str.translate(trantab)
th3s 3s str3ng 2x1mpl2....w4w!!!
#partition
>>> "my name is liaowei".partition("is")
(my name , is,  liaowei)
>>> "my name is liaowei".partition("i")
(my name , i, s liaowei)
>>> "my name is liaowei".partition("m")
(‘‘, m, y name is liaowei)
>>> "my name is liaowei".partition("na")
(my , na, me is liaowei)
>>> "my name is liaowei".partition("name")
(my , name,  is liaowei)
#大小写互换
>>> "My name is liaowei".swapcase()
mY NAME IS LIAOWEI
#填0
>>> "My name is liaowei".zfill(20)
00My name is liaowei
>>> "My name is liaowei".zfill(30)
000000000000My name is liaowei

#从左向右填充
>>> "My name is liaowei".ljust(40 ,"-")
My name is liaowei----------------------

#从右向左填充
>>> "My name is liaowei".rjust(40 ,"-")
----------------------My name is liaowei
>>> "My name is liaowei".rjust(3 ,"-")
My name is liaowei
>>> "My name is liaowei".rjust(100 ,"-")
----------------------------------------------------------------------------------My name is liaowei
>>> "My name is liaowei".rjust(14 ,"-")
My name is liaowei
>>> "My name is liaowei".rjust(20 ,"-")
--My name is liaowei

#检测一段字符串可否被当作标志符,即是否符合变量命名规则
>>> "My name is liaowei".isidentifier()
False
>>> "for".isidentifier()
True
>>> "while".isidentifier()
True
>>> "a".isidentifier()
True
>>> "in".isidentifier()
True
>>> "if".isidentifier()
True

 字典

  特点:无须的,key是唯一的。

#增加
>>> info = {}
>>> info["name"] = "liaowei"
>>> info
{name: liaowei}

#修改
>>> info
{name: liaowei}
>>> info["name"] = "abcdefg"
>>> info
{name: abcdefg}

#删除
>>> info = {name:liaowei,surname:liao}
>>> info.pop("surname")
liao
>>> del info["name"]
>>> info
{}

#查找
>>> info = {name:liaowei,surname:liao}
>>> info.get(name)
liaowei
>>> info[name]
liaowei
>>> info[name1] #当字典里没有时会报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: name1
>>> info.get(name1) #当字典里没有时不会报错

#其他
>>> info.values()
dict_values([liaowei, liao])
>>> info.keys()
dict_keys([name, surname])
>>> info.items()
dict_items([(name, liaowei), (surname, liao)])

>>> info.setdefault(name, abcdef)
liaowei
>>> info
{name: liaowei, surname: liao}
#update
>>> info
{name: liaowei, surname: liao}
>>> b={1:2,name:abcdef}
>>> info.update(b)
>>> info
{1: 2, name: abcdef, surname: liao}

#一个列表生成默认字典
>>> dict.fromkeys([1,2,3],testd)
{1: testd, 2: testd, 3: testd}

 

Python基础2 之2