python_day2_字符串

字符串

python中字符串不可变的

字符串的编码

python3字符默认就是16位Unicode编码

ord('A')
Out[26]: 65
ord('高')
Out[27]: 39640
chr(66)
Out[28]: 'B'
ord('器')
Out[29]: 22120

如何输出单引号

b="I'm a teacher"
b
Out[33]: "I'm a teacher"
print(b)
I'm a teacher

或者用转义符

a='I\'m a teacher'
print(a)
I'm a teacher

连续三个单引号,或三个双引号,可以帮助我们创建多行字符串

空字符串和len()函数
c=''
len(c)
Out[36]: 0
len('sad得我')
Out[37]: 5
b=' '
len(b)
Out[39]: 1

转义字符

\(在行尾时) 续行符
\\ 反斜杠符号
\’ 单引号
\" 双引号
\b 退格
\n 换行
\t 横向制表符

字符串拼接
a='sdf'+'efe'
a
Out[43]: 'sdfefe'
b='weew'+'213'
b
Out[45]: 'weew213'
c='wqe2'+213
Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-46-0fe44a4d3152>", line 1, in <module>
    c='wqe2'+213
TypeError: can only concatenate str (not "int") to str

也可以不用“+” 直接拼接
也可直接字符串*数字,进行复制

print('aa',end='\t')
print('bb',end='\n')
print('cc',end='\n')
>>>aa	bb
>>>cc
str()实现数字转型字符串
str(3434)
Out[49]: '3434'
【】索引
a='wqeasffdf'
a[-1]
Out[54]: 'f'
a[:,-1]
a[:-1]
Out[57]: 'wqeasffd'
replace
a='wqewqedsa'
a.replace('w','王')
Out[62]: '王qe王qedsa'
a
Out[63]: 'wqewqedsa'

字符串切片slice操作
a='ewqeqwrwsafgr'
a[2]
Out[65]: 'q'
a[1:5]
Out[66]: 'wqeq'
a[1:5:2]
Out[67]: 'we'
a[1:-3]
Out[68]: 'wqeqwrwsa'
a[1:-3:-1]
Out[69]: ''
a[1:-3:2]
Out[70]: 'wewwa'
a[1::-1]
Out[71]: 'we'
a='2134'
a='2134fagtgq'
a[-3:-8]
Out[74]: ''
a[1:-2:-1]
Out[75]: ''
a[::-1]
Out[76]: 'qgtgaf4312'

超过范围会报错

字符串分割split()和join()合并

**split()可以基于指定分割符将字符串分隔成多个子字符串(存储在列表中)。如果不指定分隔符,则默认使用空白字符(换行符/空格/指标符)。
在这里插入图片描述

扫描二维码关注公众号,回复: 11123588 查看本文章
import time
time01 =time.time()   #起始时刻
a=' '
for i in range(1000000):
    a+='sxt'
time02 =time.time()         #终止时刻
print("运算时间:" + str (time02-time01))
time03 =time.time()   #起始时刻

li=[]
for i in range(1000000):
    li.append('sxt')

a=''.join(li)
time04 =time.time()   #起始时刻
print("运算时间:" + str (time04-time03))


>>>运算时间:0.4607670307159424
>>>运算时间:0.10471916198730469

因为字符串定义后不可变,每次循环一次要创建个对象,而列表不用重新创建对象,直接在后面添加即可,所以是时间短很多。

字符串驻留机制和字符串比较

字符串驻留:仅保存一份相同且不可变字符串的方法,不同的值被存放在字符串驻留池中,
Python支持字符串驻留机制,对于符合标识符规则的字符串(仅包含下划线(_)、字母和数字)会启用字符串驻留机制。

a='1_2ere'
b='1_2ere'
a is b
Out[5]: True
c='231er'
d='231er'
c is d
Out[8]: True
e='3&34'
f='3&34'
e is f
Out[11]: False

成员操作符:
in
not in
判断某个字符串是否在字符串全体中

常用查找方法

大小写转换:

str = "www.runoob.com"
print(str.upper())          # 把所有字符中的小写字母转换成大写字母
print(str.lower())          # 把所有字符中的大写字母转换成小写字母
print(str.capitalize())     # 把第一个字母转化为大写字母,其余小写
print(str.title())          # 把每个单词的第一个字母转化为大写,其余小写 
>>>
WWW.RUNOOB.COM
www.runoob.com
Www.runoob.com
Www.Runoob.Com

常用查找方法:

a='新型冠病毒已经在意大利传染严重了,导致16000人感染'
len(a)
Out[13]: 27
a.startswith('新型')
Out[14]: True
a.endswith('感染')
Out[15]: True
a.find('病')
Out[16]: 3
a.rfind('病')
Out[17]: 3
a.count('了')
Out[18]: 1
a.count('0')
Out[19]: 3
a.find('0')
Out[20]: 21
a.rfind('0')
Out[21]: 23

格式排版:
center(),ljust(),rjust()分别对应居中、左对齐、右对齐

>>> a='sdf'
>>> a.center(5,'*')
'*sdf*'

5是字符串长度,‘*’是填充字符,不指定填充字符,默认为空格

字符串格式化

format()基本用法

a='名字是:{0},年龄是:{1}'
b=a.format('ws','24')
b
Out[31]: '名字是:ws,年龄是:24'
c='名字是:{name},年龄是:{age}'
d=c.format(age=24,name='ws')
d
Out[36]: '名字是:ws,年龄是:24'

填充与对齐:
填充常跟对齐一起使
^、<、>分别是居中、左对齐、右对齐,后面带宽度
:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填

>>> "{:+<5}".format("123")
'123++

数字格式化:
浮点用f,整数用d
在这里插入图片描述

可变字符串

字符串原则上是不可变的,但是当我想原地修改字符串时,可以用io.String();或者用array模块。

import io
s='wsnihao'
sio=io.stringio(s)
sio=io.StringIO(s)
sio
Out[6]: <_io.StringIO at 0x1f5d5ca4678>
sio.getvalue()
Out[7]: 'wsnihao'
sio.seek(3)
Out[8]: 3
sio.write('2')
Out[9]: 1
sio.getvalue()
Out[10]: 'wsn2hao'
发布了22 篇原创文章 · 获赞 1 · 访问量 470

猜你喜欢

转载自blog.csdn.net/ws297933371/article/details/104805835