标签:tool idt 技术分享 with index 索引 size nes enc
字符串
字符串不可更改
1,字符串格式化
>>> name=‘lily‘ >>> age=20 >>> print(‘my name is ‘+ name + " , I‘m " + str(age) + " years old") #字符串拼接,不建议 my name is lily , I‘m 20 years old >>> print("my name is %s, I‘m %s years old" %(name,age)) #字符串格式化 ,推荐 my name is lily, I‘m 20 years old
format
>>> print(‘my name is {} ,and age is {}‘.format(‘lily‘,22))
my name is lily ,and age is 22
>>> print(‘my name is {0} , and age is {1}‘.format(‘lily‘,22))
my name is lily , and age is 22
>>> print(‘my name is {name}, and age is {age}‘.format(age=22,name=‘lily‘))
my name is lily, and age is 22
2,常用的字符串功能
可通过 dir(obj) 查看方法
索引 obj[0] obj[-1] 切片 obj[1:4] 长度 len(obj) 移除空白 obj.lstrip() 去头空白 obj.strip() 两头 obj.rstrip() 去尾部空白 分割 obj.split()
大小写 obj.title()首字母大写 obj.upper() 大写 obj.lower() 小写
join() 和split()方法
join()方法是针对一个字符串而调用的,并且传入一个列表值
>>> ‘,‘.join(‘hello world‘) ‘h,e,l,l,o, ,w,o,r,l,d‘ >>> ‘ ‘.join([‘my‘,‘name‘,‘is‘,‘lily‘]) ‘my name is lily‘
split()方法做的事情正好相反:它针对一个字符串调用,返回一个字符串列表。
>>> ‘hi python hello world‘.split() [‘hi‘, ‘python‘, ‘hello‘, ‘world‘] >>> >>> ‘mynameislilymyageis22myaimispython‘.split(‘my‘) [‘‘, ‘nameislily‘, ‘ageis22‘, ‘aimis
str方法:
def capitalize(self): #首字母转换成大写 >>> ‘hello world‘.capitalize() ‘Hello world‘ def casefold(self): # 全部转换成小写 >>> ‘HELLO world‘.casefold() ‘hello world‘ def center(self, width, fillchar=None): # 居中 >>> ‘hello world‘.center(20,‘-‘) ‘----hello world-----‘ def count(self, sub, start=None, end=None): # 计数 >>> s=‘hello world!‘ >>> s.count(‘l‘) >>> s.count(‘l‘,1,3) def encode(self, encoding=‘utf-8‘, errors=‘strict‘): #编码 把str变成二进制 编码(encode):string-->bytes 解码(decode):bytes-->string >>> ‘hello‘.encode() b‘hello‘ >>> >>> b‘hello‘.decode() ‘hello‘ def endswith(self, suffix, start=None, end=None): #以什么结尾 >>> ‘hello‘.endswith(‘o‘) True >>> ‘hello world‘.endswith(‘w‘,1,7) #指定起始位置 True def expandtabs(self, tabsize=8): #字符串中的 tab (‘\t‘)转为空格 >>> s ‘hello\tworld‘ >>> s.expandtabs() ‘hello world‘ >>> s.expandtabs(20) ‘hello world‘ def find(self, sub, start=None, end=None): #找到时返回索引,找不到时返回-1.区别于index >>> ‘hello‘.find(‘e‘) >>> ‘hello‘.find(‘x‘) -1 def format(self, *args, **kwargs): # 格式化字符串 >>> ‘my name is {1} age is {0}‘.format(22,‘lily‘) ‘my name is lily age is 22‘ >>> ‘my name is {name}‘.format(name=‘lily‘) ‘my name is lily‘ def format_map(self, mapping): # real signature unknown; restored from __doc__ """ S.format_map(mapping) -> str Return a formatted version of S, using substitutions from mapping. The substitutions are identified by braces (‘{‘ and ‘}‘). def index(self, sub, start=None, end=None): # 找到时返回索引,找不到时报错 >>> ‘hello‘.index(‘e‘) >>> ‘hello‘.index(‘y‘) raceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found def isalnum(self): # 都是字母和数字组成 def isalpha(self): # 都是由字母组成 def isdigit(self): # 都是由数字组成 def isdecimal(self): #是否只包含十进制字符 def islower(self): #是否由小写字母组成 def isnumeric(self): # 是否只由数字组成,只适用于unicode对象 def isprintable(self): #是否都是可打印字符 def isspace(self): #是否只由空格组成 def isidentifier(self): # real signature unknown; restored from __doc__ def istitle(self): # 所有单词拼写首字母为大写,且其他字母为小写。 >>> s=‘Hello World‘ >>> s.istitle() True def isupper(self): #所有单词大写 >>> s=‘HI123‘ >>> s.isupper() True def join(self, iterable): # 用指定的字符连接序列中的元素生成一个新的字符串 >>> l=[‘a‘,‘b‘,‘c‘,‘d‘] >>> ‘-‘.join(l) ‘a-b-c-d‘ def ljust(self, width, fillchar=None): #居左 >>> s= ‘helloworld‘ >>> s.ljust(20,‘*‘) ‘helloworld**********‘ def lstrip(self, chars=None): # 去除开头的空格 def maketrans(self, *args, **kwargs): # real signature unknown def partition(self, sep): #按照指定分割符将字符串分割 >>> ‘hello‘.partition(‘l‘) (‘he‘, ‘l‘, ‘lo‘) def split(self, sep=None, maxsplit=-1): # 通过指定分隔符对字符串切片 >>> ‘abacadae‘.split(‘a‘) [‘‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘] >>> ‘a:b:c‘.split(‘:‘) [‘a‘, ‘b‘, ‘c‘] def replace(self, old, new, count=None): # old替换成new,次数(默认全部替换) >>> ‘hello‘.replace(‘l‘,‘x‘) ‘hexxo‘ >>> ‘hello‘.replace(‘l‘,‘x‘,1) ‘hexlo‘ def splitlines(self, keepends=None): # real signature unknown; restored from __doc__ def strip(self, chars=None): #去掉前后空格 """ S.strip([chars]) -> str Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. """ return "" def swapcase(self): # 大小写转换 >>> ‘helloWORLD‘.swapcase() ‘HELLOworld‘ def title(self): # 将首字母转换成大写 其他转成小写 >>> ‘HELLO WORLD‘.title() ‘Hello World‘ def lower(self): # 全部转换成小写 def upper(self): # 全部转换成大写 >>> ‘Hello World‘.upper() ‘HELLO WORLD‘ def translate(self, table): #根据 table 的规则(可以由 str.maketrans(‘a‘, ‘b‘) 定制)转换字符串中的字符 >>> s ‘hello world‘ >>> s.translate(s.maketrans(‘l‘,‘x‘)) ‘hexxo worxd‘ def zfill(self, width): # 返回指定长度的字符串,原字符串右对齐,前面填充0
标签:tool idt 技术分享 with index 索引 size nes enc
原文地址:https://www.cnblogs.com/littledeveloper/p/9952383.html