Python数据分析实战3.4-文本序列str常用操作【python】

【课程3.4】 文本序列str常用操作

字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串
字符串也是序列:文本序列

1.字符串引号

str1 = "abc"
str2 = 'abc'
str3 = 'my name is "fatbird"'
# 双引号单引号无区别,但文本中有引号的时候要相互交替使用

str4 = '''hello! how are you?
          I`m fine, thank you.'''
# 需要多行字符串时候用三引号 ''' ''',""" """

2.转义字符:\

print('\'', '\"')  # \',\" :分别输出单引号,双引号
print('hello\nhow do you do?')  # \n:空行
print('\\')  # 输出\,所以文件路径用“/”或者“\\”

---------------------------------------------------------------------
' "
hello
how do you do?
\

3.序列通用功能

print('a' in 'abc')  # in/not in :判断是否存在
print('我很帅' + "没错")  # 文本连接
print('handsome '*4)  # 文本复制

st = 'abcdefg'
print(st[2],st[-1])
print(st[:2])
print(st[::2])
---------------------------------------------------------------------

4.索引、切片、步长

print(st.index('g'))  # .index()方法
print('st长度为:',len(st))  # 计算字符串长度(思考这里能否把逗号换为"+")
---------------------------------------------------------------------
True
我很帅没错
handsome handsome handsome handsome 
c g
ab
aceg
6
st长度为: 7

5.字符串常用功能

st = "i`m handsome!"
st2 = st.replace('handsome','ugly')
print(st)
print(st2)
st = 'hahaha'
st2 = st.replace('ha','he',2)
print(st2)
# str.replace(old,new,count):修改字符串,count:更换几个

st = "poi01,116.446238,39.940166"
lst = st.split(',')
print(lst)
# str.split(obj):拆分字符串,生成列表

lst = ['poi01', '116.446238', '39.940166']
m = '-'
st = m.join(lst)
print(st)
# str.join():连接字符串,对象为列表,join和split是一对相反的操作

st = 'abcdefg'
print(st.startswith('a'), st.endswith('f'))
# str.startswith(“str”)  判断是否以“str”开头;str.endswith (“str”)  判断是否以“str”结尾

st = 'aBDEAjc kLM'
print(st.upper())  # 全部大写
print(st.lower())  # 全部小写
print(st.swapcase())  # 大小写互换
print(st.capitalize())  # 首字母大写

st = '1234567'
print(st.isnumeric())  # 如果 string 只包含数字则返回 True,否则返回 False.

st = 'DNVAK'
print(st.isalpha())  # 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False

st = 'avd   '
print(st.rstrip())  # 删除字符末尾的空格
---------------------------------------------------------------------
FATBIRD is 好人
this is 4
this is hehe
this is 4.200000

6.格式化字符:数字格式化的那些坑

m = 3.1415926 
print("pi is %f"  %m)
print("pi is %.2f" %m)
# 我只想输出2位小数:%.2f,此处是四舍五入!

m = 10.6
print("pi is %i"  %m)
print("pi is %.0f" %m)
# 区别:%i 不四舍五入,直接切掉小数部分,输出整数,利用%0.f进行四舍五入

m = 100 
print("have fun %+i"  %m)
print("have fun %.2f"  % -0.01)
# 显示正号,负号根据数字直接显示

m = 100 
print("have fun % i"  %m)
print("have fun % +i"  %m)
print("have fun % .2f"  %-0.01)
# 加空格,空格和正号只能显示一个

m = 123.123123123 
print("have fun %.2e"  %m)
print("have fun %.4E"  %m)
# 科学计数法 %e  %E

m1 = 123.123123123
m2 = 1.2
print("have fun %g"  %m1)
print("have fun %g"  %m2)
# 小数位数少的时候自动识别用浮点数,数据复杂的时候自动识别用科学计数法
---------------------------------------------------------------------
User ID: root
a 呵呵 b
abc 
 abca 

我的工作是设计
abcdef abc{}
4.123000 
 4.12 
 4.123000e+00 
 100 
 412.300000% 
 10

7.更强大的格式化方法.format

print("User ID: {0}".format("root"))
print("{} 呵呵 {}".format("a","b"))
# {} 这里代表占位符,其中可以有数字也可以没有

print("{}{}{}".format('a','b','c'),'\n',
     "{0}{1}{2}{0}".format('a','b','c'),'\n')
#print("{}{}{}{}".format('a','b','c'))
# {}和{0}的区别:都是占位符,后者有了明确指定

print("我的工作是{work}".format(work = '设计'))
# 也可以用变量来指示

x="abc{}"
a = x.format("def")
print(a,x)
# .format()生成新的值吗??

print("{:f}".format(4.123),'\n',
 "{:.2f}".format(4.123),'\n',
 "{:e}".format(4.123),'\n',
 "{:.0f}".format(99.9),'\n',
 "{:%}".format(4.123),'\n',
 "{:d}".format(10))
---------------------------------------------------------------------
User ID: root
a 呵呵 b
abc 
 abca 

我的工作是设计
abcdef abc{}
4.123000 
 4.12 
 4.123000e+00 
 100 
 412.300000% 
 10

小作业
① 三引号创建一个有提行的文本,print()函数输出
② 申明一个字符串变量,值为一个文件路径,注意操作习惯
③ 以下输出分别是多少:33+“22”; 33+int(“22”); “22” + str(55);
④ m=“a,b,c”,m.split()会输出什么结果?(不加",")
⑤ 再回想一下:.split和.join的输出分别是什么类型的数据
⑥ 如果我想打%怎么办?
⑦ 这样书写正确吗,为什么? print(“abc%s”) % “nn”
⑧ 这样书写正确吗,为什么? print(245%f % 123)
⑨ “我的工作是…我喜欢…” 把…替换成正确的内容

4.报错
5.split 和 join 是相反的操作,split吧字符串切割成列表,join把列表连接成字符串
6.%%
7.不正确
8.

发布了36 篇原创文章 · 获赞 17 · 访问量 6274

猜你喜欢

转载自blog.csdn.net/qq_39248307/article/details/105451323