python——字符串与序列
来源:互联网 发布:穆勒plc手动编程 编辑:程序博客网 时间:2024/05/29 19:11
字符串
python中没有字符的概念==与元组一样,不能直接修改
支持拼接版的修改
>>> str1='asdf'>>> str1.capitalize()'Asdf'>>> str1'asdf'>>> str1='DSADas'>>> str1.casefold()'dsadas'>>> str1'DSADas'>>> str1.center(40)' DSADas '>>> str1'DSADas'>>> str1.count('a')1>>> str1.count('a',true)Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> str1.count('a',true)NameError: name 'true' is not defined>>> str1.endwith(('s')KeyboardInterrupt>>> str1.endswith('s')True>>> str1='i\tlove\tprogoram'>>> str1.expandtabs()'i love progoram'>>> str1.find('pro')7>>> str1.find('f')-1>>> str1.index('f')Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> str1.index('f')ValueError: substring not found>>> str1.index('pro')7isalnum()=isalpha()+isdigit()>>> str2='周雨佳'>>> str2.islower() 至少包含一个区分大小写的字符,且这些字符都是小写,返回trueFalseistitle()所有单词首字母都是大写,其他字母都是小写isupper()所有字母都是大写>>> str2.join('dsds')'d周雨佳s周雨佳d周雨佳s'>>> str3=' fdsfsd sfds '>>> str3.lstrip()'fdsfsd sfds '>>> str3.rstrip()' fdsfsd sfds'>>> str3.partition('ds')(' f', 'ds', 'fsd sfds ')>>> str3.replace('f','z',2)' zdszsd sfds 'rfind rindex rstrip startswith strip一样的>>> str3.split('')Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> str3.split('')ValueError: empty separator>>> str3.split()['fdsfsd', 'sfds']>>> str3' fdsfsd sfds '>>> str3.strip('s')' fdsfsd sfds '>>> str4='FFSDds'>>> str4.swap()Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> str4.swap()AttributeError: 'str' object has no attribute 'swap'>>> str3.title()' Fdsfsd Sfds '>>> str3.translate(str.maketrans('s','z'))' fdzfzd zfdz '>>> str3.maketrans('s','z'){115: 122}>>> str3.zfill(30)'00000000000000 fdsfsd sfds '
格式化字符串
>>> str1='asdf'>>> str1.capitalize()'Asdf'>>> str1'asdf'>>> str1='DSADas'>>> str1.casefold()'dsadas'>>> str1'DSADas'>>> str1.center(40)' DSADas '>>> str1'DSADas'>>> str1.count('a')1>>> str1.count('a',true)Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> str1.count('a',true)NameError: name 'true' is not defined>>> str1.endwith(('s')KeyboardInterrupt>>> str1.endswith('s')True>>> str1='i\tlove\tprogoram'>>> str1.expandtabs()'i love progoram'>>> str1.find('pro')7>>> str1.find('f')-1>>> str1.index('f')Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> str1.index('f')ValueError: substring not found>>> str1.index('pro')7>>> str2='周雨佳'>>> str2.islower()False>>> str2.join('dsds')'d周雨佳s周雨佳d周雨佳s'>>> str3=' fdsfsd sfds '>>> str3.lstrip()'fdsfsd sfds '>>> str3.rstrip()' fdsfsd sfds'>>> str3.partition('ds')(' f', 'ds', 'fsd sfds ')>>> str3.replace('f','z',2)' zdszsd sfds '>>> str3.split('')Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> str3.split('')ValueError: empty separator>>> str3.split()['fdsfsd', 'sfds']>>> str3' fdsfsd sfds '>>> str3.strip('s')' fdsfsd sfds '>>> str4='FFSDds'>>> str4.swap()Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> str4.swap()AttributeError: 'str' object has no attribute 'swap'>>> str3.title()' Fdsfsd Sfds '>>> str3.translate(str.maketrans('s','z'))' fdzfzd zfdz '>>> str3.maketrans('s','z'){115: 122}>>> str3.zfill(30)'00000000000000 fdsfsd sfds '>>> "{0} love{1}.{2}".format("i","am",'misszhou')'i loveam.misszhou'>>> "{a}{b}{c}".format(a="i",b="am",c="misszhou")'iammisszhou'>>> "{a}{b}{0}".format(a="i",b="am","misszhou")SyntaxError: positional argument follows keyword argument>>> print('\tz')z>>> "{{0}}".format("no")'{0}'>>> '{0:.lf}{1}'.format(27.2324,'GB')Traceback (most recent call last): File "<pyshell#45>", line 1, in <module> '{0:.lf}{1}'.format(27.2324,'GB')ValueError: Format specifier missing precision>>> '{0:.1f}{1}SyntaxError: EOL while scanning string literal>>> '{0:.1f}{1}'.format(27.2324,'GB')'27.2GB' %c %s %d o x X f e E g G(根据数字大小选择f e)>>> '%c' %97'a'>>> '%c%c%c' % {97,98,99}Traceback (most recent call last): File "<pyshell#49>", line 1, in <module> '%c%c%c' % {97,98,99}TypeError: %c requires int or char>>> '%c%c%c' % (97,98,99)'abc'>>> '%s' % 'jkfjdk''jkfjdk'>>> '%d+%d=%d' % (4,5,4+5)'4+5=9'>>> '%o' % 12'14'>>> '%x' %10'a'>>> '%f" %23.43423SyntaxError: EOL while scanning string literal>>> '%f'%2343'2343.000000'>>> '%e'%342443r5r32SyntaxError: invalid syntax>>> '%e'%432434'4.324340e+05'格式化操作符辅助指令>>> '%10d'%5' 5'>>> '%-10d'%4'4 '>>> '%+d'%4'+4'>>> '%+d'%-5'-5'>>> '%#o'%432'0o660'>>> '%#X'%4543'0X11BF'>>> '%#d'%4342'4342'>>> '%010d'%4342'0000004342'>>> '%-010d'%43543'43543 '
转义字符也是一样用的
\t \n \r
序列
列表、元组、字符串的共同点
都可以通过索引得到每一个元素
默认索引值是0
可以通过分片的方法得到一个范围内的元素集合
有很多相同的操作符
list
>>> help(list)Help on class list in module builtins:class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | (迭代:重复反馈过程的活动,目的通常是为了接近和达到所需的目标或结果,每次重复叫做迭代,每次迭代的结果作为下一次的初始值) | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __gt__(self, value, /) | Return self>value. | | __iadd__(self, value, /) | Implement self+=value. | | __imul__(self, value, /) | Implement self*=value. | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value.n | | __ne__(self, value, /) | Return self!=value. | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __repr__(self, /) | Return repr(self). | | __reversed__(...) | L.__reversed__() -- return a reverse iterator over the list | | __rmul__(self, value, /) | Return self*value. | | __setitem__(self, key, value, /) | Set self[key] to value. | | __sizeof__(...) | L.__sizeof__() -- size of L in memory, in bytes | | append(...) | L.append(object) -> None -- append object to end | | clear(...) | L.clear() -> None -- remove all items from L | | copy(...) | L.copy() -> list -- a shallow copy of L | | count(...) | L.count(value) -> integer -- return number of occurrences of value | | extend(...) | L.extend(iterable) -> None -- extend list by appending elements from the iterable | | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present. | | insert(...) | L.insert(index, object) -- insert object before index | | pop(...) | L.pop([index]) -> item -- remove and return item at index (default last). | Raises IndexError if list is empty or index is out of range. | | remove(...) | L.remove(value) -> None -- remove first occurrence of value. | Raises ValueError if the value is not present. | | reverse(...) | L.reverse() -- reverse *IN PLACE* | | sort(...) | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None>>> a=list()>>> a[]>>> b='i am misszhou'>>> b=list(b)>>> b['i', ' ', 'a', 'm', ' ', 'm', 'i', 's', 's', 'z', 'h', 'o', 'u']>>> c=(1,1,3,5,8)>>> c=list(c)>>> c[1, 1, 3, 5, 8]>>> help(tuple)Help on class tuple in module builtins:class tuple(object) | tuple() -> empty tuple | tuple(iterable) -> tuple initialized from iterable's items | | If the argument is a tuple, the return value is the same object. | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(self, key, /) | Return self[key]. | | __getnewargs__(...) | | __gt__(self, value, /) | Return self>value. | | __hash__(self, /) | Return hash(self). | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self<value. | | __mul__(self, value, /) | Return self*value.n | | __ne__(self, value, /) | Return self!=value. | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __repr__(self, /) | Return repr(self). | | __rmul__(self, value, /) | Return self*value. | | count(...) | T.count(value) -> integer -- return number of occurrences of value | | index(...) | T.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present.>>> len(a)0>>> len(c)5>>> c[1, 1, 3, 5, 8]>>> max(c)8>>> max(b)'z'>>> numbers=[1,18,13,0,-23,4,-342]>>> max(numbers)18>>> min(numbers)-342>>> chars='1232342'>>> min(chars)'1'>>> tuple1=(1,2,4,5,7,9)
必须是相同类型的才有max min 数字和字符不能比较
0 0
- python——字符串与序列
- Python序列——字符串
- Python序列——字符串
- Python数据结构:序列——字符串
- 3. Python中的序列——字符串
- 【人生苦短,我用Python】Python免费精品课连载(2)——序列、列表、元组与字符串
- 【人生苦短,我用Python】Python免费精品课连载(2)——序列、列表、元组与字符串
- Python数据类型——序列(字符串,列表,元组)
- Python数据类型——序列(列表、元组、字符串)
- Python基础——核心数据类型_序列_字符串
- Python学习教程(三)——序列之字符串
- Python序列之字符串
- python序列:字符串
- python字符串和序列
- Python序列之字符串
- 我与python约个会:17. 字符串详解及序列类型
- Python——序列
- Python——序列
- 安装laravel
- HDU 3065 AC自动机 模板题
- 获取Assets文件值
- RedHat RHEL 安装gcc的方法
- MySQL优化必须调整的10项配置
- python——字符串与序列
- Storm在Zookeeper中的目录结构
- [2016ACM多校] HDU5787 数位DP
- 跨平台开发框架Ionic学习之路------3(使用barcodescanner扫描)
- 获取本地图片
- web.xml中classpath和classpath*的区别
- 编程助手JavaScript学习库-面向对象编程笔记
- HDU5795 A Simple Nim
- http头文件