版权声明:https://blog.csdn.net/lancer777?assign_skin=skin-blackboard https://blog.csdn.net/lancer777/article/details/82802680
字符串
字符串的创建:
创建形式:
单引号,双引号,三引号
确定数据类型:
>>> a="qwee"
>>> a
'qwee'
>>> type(a)
<class 'str'>
>>> isinstance(a,str)
True
>>>
字符串的增删改:
字符串不支持增加,删除,修改
列表中可以用下标的方式来修改元素
例:
l=[1,2,3]
l[0]=666
print(l)
[666,2,3]
字符串的访问:
1.用下标访问:
例: >>> a=‘qweqwqwqwwqw’
>>> a[6]
‘w’
2.用for循环可以进行遍历
可以用for循环遍历解决一个数的各位相加和问题
3.字符串可以切片
a=b[开始下标号:结束下标号]
a=b[-2:-5] #左闭右开
字符串的方法:
1.capitalize() 用.的方法调用
返回一段首字母为大写的字符串
2.casefold() 用.的方法调用
返回全小写的字符串
3.center() 用.的方法调用
返回居中的字符串,整体长可以在参数中设定
4.count() 用.的方法调用
查询字符串中,字符的数量
eg:>>> a.count('w') 查 w 的数量
6
eg:也可以指定位置查询
a.count('w',4,6) 区间左闭右开
在4和6之间查有几个 'w'
若查不到返回0 不会报错
5, endswith('argument')
判断该字符串以 argu内容结尾 结果返回bool值
6. startswith('argument')
判断该字符串以 开头 结果返回bool值
7. find(‘str’,start,end)其中start end可以省略不写
返回查找某字符在该字符串指定范围内的下标,如果能找到,则返回他的下标,如果找不到返回-1 不会报错
8. index()
可以在一个范围内查找
>>> a.find('l')
2
>>> a.find('l',0,5)
2
>>> a.find('l',0,2)
-1
>>> a.find('ll',0,6)
2
>>> a.index('o')
4
>>> a.index('l')
2
>>> a.index('z')
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
a.index('z')
ValueError: substring not found
>>> a.index('o',0,7)
4
>>> a.index('o',0,4)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
a.index('o',0,4)
ValueError: substring not found
>>> a.index('ll',0,4)
9.isalnum()
判断字符串是否都由数字和字母组成。返回一个bool值
10.isalpha()
判断字符串是否由纯字母组成。 返回一个bool值
11.isdecimal()
是否都是十进制数
12.isdigit()
判断字符串中是否都是数字
13.isnumeric()
判断字符串中是否都是数字字符 可以识别汉字
>>> a='四'
>>> a.isdigit()
False
>>> a.isdecimal()
False
>>> a.isnumeric()
True
14.islower()
判断字符串中的字母是否都为小写 返回True
15.isspace()
判断字符串中是否全为空格
>>> a.isspace()
False
>>> a=' '
>>> a.isspace()
True
>>> a=''
>>> a.isspace()
False
16.istitle()
False
>>> a='abc'
>>> a.istitle()
False
>>> a='Abc'
>>> a.istitle()
True
>>> a='aBc'
>>> a.istitle()
False
>>>
判断字符串中首字母是否为大写
17.isupper()
判断字符串是否全为大写
18.join()
将一个原有字符串插入到一个可迭代对象当中。
>>> a='abcdefgh'
>>> a.join('111')
'1abcdefgh1abcdefgh1'
>>> a='qwerrttyuu'
>>> a.join('111111111')' 1qwerrttyuu1qwerrttyuu1qwerrttyuu1qwerrttyuu1qwerrttyuu1qwerrttyuu1qwerrttyuu1qwerrttyuu1'
19.ljust(填充后的总长度)
将原有字符串左对齐,其余字符用空格填充
20.
rjust(填充后的总长度)
将原有字符串右对齐,其余字符用空格填充
21.
lstrip()
将左侧空格清空
22.
rstrip()
23.replace(旧的,新的)
可以将原来的字符串替换成新的字符串
可以选定要替换几个
即replace(旧的,新的,个数)
可以通过这种方式来只替换一部分的字符
字符串的格式化输出
1.format()
形式:1位置参数
例:
‘{0}{1}’,format('hello','world')
形式: 2关键字参数
‘{a}{b}’,format(b='hello',a='world')
形式:3位置参数和关键字参数混用
'{0}{a}{1}',format('hello','world',a='my')
hellomyworld
注释:位置参数要在关键字参数之前,指的是format中的位置参数与关键字参数
2.‘{}’可以被'{}'转移如果想打{hello},那么
例
>>> '{0}'.format('hello')
'hello'
>>> '{{0}}'.format('hello')
'{0}'
>>> '{{{0}}}'.format('hello')
'{hello}'
格式化输出:
%c 字符
%d 整形
%f 浮点型 输出小数点后6位
>>> '%c'%97
'a'
>>> '%d'%97
'97'
>>> '%f'%12.1234567
'12.123457'
%e e计法 科学计数法
>>> '%e'%123456
'1.234560e+05'
>>> '%e'%123456712345671234567
'1.234567e+20'
%g 按需求转化位定点数或者e计法
数很大的时候会转化为%e
小数位很大的时候转化为%f (总共6位)
>>> '%g'%12
'12'
>>> '%g'%123456789
'1.23457e+08'
>>> '%g'%12.3456789
'12.3457'
%o转化为八进制
>>> "%o"%10
'12'
>>> "%o"%999
'1747'
%x转化为十六进制
>>> "%x"%8
'8'
>>> "%x"%10
'a'
>>> "%x"%16
'10'
>>> "%x"%17
'11'