数据类型-字符串:
特性:有序和不可变
字符串可以加和乘 拼接
创建:
对于字符串,一下表示:
s.capitalize()开头大写其余小写
'Helloworld!'
s.casefold()全部小写
'helloworld!'
s.center(50,'*')50是长度,*是填的东西
'*******************HelloWorld!*******************'
s.count('o')统计‘o’有几个,统计字符串中字符的个数
2
s.count('o',0,5)统计从索引0开始到第5个‘o’的个数
1
s.endswith('!')判断是不是以‘!’结尾
True
s.endswith('o')
False
s1='a\tb' \t tab键
print(s1)
a b
s1.expandtabs()扩展tab键
'a b'
s1.expandtabs(1)1是size a到b的长度
'ab'
s1.expandtabs(60)60如上
'a b'
s.find('o')查找字符串返回它的索引值
4
s.find('os')查不到字符串返回 -1
-1
s.find('o',0,3)也可以起始查找,找不到返回-1
-1
s.find('o',0,5)找到返回索引值
4
s.rfind('o')
7
s3='my name is {0},i am {1} years old'
s3
'my name is {0},i am {1} years old'
s3.format('albert',23)
'my name is albert,i am 23 yearsold'
s3='my name is {1},i am {1} years old'
s3.format('albert',23)括号里面按索引对应上面大括号数字
'my name is 23,i am 23 years old'
s3='myname is {name},i am {age} years old'
s3.format(name='albert',age=23)
'myname is albert,i am 23 years old'
s.index('o')返回字符串索引
4
s.index('o',5,9)可起始
7
也可以rindex
'66a'.isalnum()判断是否属于阿拉伯数字、字母
True
'66a"'.isalnum()
False
'ddas'.isalpha()只表示字母
True
'ddas5'.isalpha()
False
's'.isdecimal()
False
'16843'.isdecimal()判断是否是整数
True
'54'.isdigit()和上面的几乎一样
True
'54.2'.isdigit()
False
'5'.isidentifier()
False
'aa5'.isidentifier()判断是否是合法变量名
True
'sdfasdf'.islower()判断是否都是小写
True
'sdfasdFf'.islower()
False
'35743d'.isnumeric()
False
'35743'.isnumeric()判断是否只有数字在里面
True
Linux系统上一切皆文件即2进制流,不能被打印。文本文件或字节格式数据可打印
'sdf'.isprintable()判断是否可被打印
True
''.isspace()判断是否空格
False
'dsf sdf'.isspace()
False
' '.isspace()
True
s
'Hello World!'
s.istitle()判断是否为标题,即每个字母前大写
True
s ='Hello world!'
s.istitle()
False
s.isupper()判断是否都是大写
False
'SAFDAS'.isupper()
True
names = ['albert','yangchanglong','杨昌龙']
''.join(names)以什么格式拼接展现
'albertyangchanglong杨昌龙'
'*'.join(names)
'albert*yangchanglong*杨昌龙'
s.ljust(20)从左边开始把字符变成20个长度
'Hello world! '
s.ljust(20,'#')
'Hello world!########'
s.ljust(30,'#')这个是变成30
'Hello world!##################'
s.ljust(30,'-')
'Hello world!------------------'
len(s.ljust(30,'-')) len()字符串长度
30
s.rjust(30,'-')
'------------------Hello world!'
s
'Hello world!'
s.lower()全部小写
'hello world!'
s.upper()全部大写
'HELLO WORLD!'
s= '\n Hello World ! '
s
'\n Hello World ! '
s.strip()去掉字符串两边的空格和换行还有Tab键 \t
'Hello World !'
s.lstrip()左
'Hello World ! '
s.rstrip()右
'\n Hello World !'
str_in='abcdef'
str_out='*&^%$#'
str.maketrans(str_in,str_out)属于自己的密码映射表
{97: 42, 98: 38, 99: 94, 100: 37, 101: 36,102: 35}
table= str.maketrans(str_in,str_out)
table
{97: 42, 98: 38, 99: 94, 100: 37, 101: 36,102: 35}
s
'\n Hello World ! '
s.translate(table) 翻译
'\n H$lloWorl% ! '
'asdfxvcb'.translate(table)
'*s%#xv^&'
s
'\n Hello World ! '
s.partition('o')以o为节点将字符分割
('\n Hell', 'o', ' World ! ')
s.partition(' ')
('\n', ' ', 'Hello World ! ')
s.partition('W')
('\n Hello ', 'W', 'orld ! ')
s
'\n Hello World ! '
s.replace('H','h')替换
'\n hello World ! '
s.replace('o','-')
'\n Hell- W-rld ! '
s.replace('o','-',1)
'\n Hell- World ! '
s.replace('o','-',2)
'\n Hell- W-rld !
s
'Hello World !'
s.split()
['Hello', 'World', '!']
s.split('o')
['Hell', ' W', 'rld !']
s.rsplit('o')以什么为分割,变成数组
['Hell', ' W', 'rld !']
s.rsplit('o',1)
['Hello W', 'rld !']
s='a\nb\nalbert\nc'
s.splitlines()按行来分
['a', 'b', 'albert', 'c']
s
'Hello World'
s.startswith('He')判断以什么什么开始
True
s.startswith('Hell')
True
s.startswith('Helle')
False
s.endswith('World')判断以什么结束
True
s.endswith('rld')
True
s.endswith('rl')
False
s
'Hello World'
s.swapcase()小写换大写
'hELLO wORLD'
s
'hELLO wORLD'
s.title()变成title
'Hello World'
s
'hELLO wORLD'
s.zfill(30)用0把字符串补充到30
'0000000000000000000hELLO wORLD'
以下几个经常用:
format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22'
'9'.isdigit() 是否整数
name.count('lex') 统计 lex出现次数
name.center(50,"-") 输出 '---------------------Alex Li----------------------'
name.find('A') 查找A,找到返回其索引, 找不到返回-1
"|".join(['albert','jack','jery']) 'albert|jack|jery'
>>> "alex li, chinese name is lijie".replace("li","LI",1) 'alex LI, chinese name is lijie'
来源:https://www.cnblogs.com/AlbertY/p/8786246.html