python基础数据类型之字符串操作
发布时间:2020-12-17 00:22:36 所属栏目:Python 来源:网络整理
导读:1.字符串切片 ps:字符串是不可变的对象,所以任何操作对原字符 是不会有任何影响的 s1 = (s1[1 (s1[2 (s1[3 (s1[4 (s1[5 (s1[6 (s1[7 (s1[8 (s1[9]) (s1[-1]) (s1[-2]) 语法一: str[start: end]规则: 从start开始截取. 截取到end位置. 但不包括end s2 = (s2[0
1.字符串切片ps:字符串是不可变的对象,所以任何操作对原字符 是不会有任何影响的 s1 =
(s1[1(s1[2(s1[3(s1[4(s1[5(s1[6(s1[7(s1[8(s1[9])
(s1[-1])
(s1[-2])
语法一: str[start: end]规则: 从start开始截取. 截取到end位置. 但不包括end s2 =
(s2[0:3])
(s2[6:8])
(s2[6:9])
(s2[6:10])
(s2[4:])
(s2[-1:-5])
(s2[-5:-1])
(s2[-5:])
(s2[:-1])
(s2[:])
语法二: str[start:end:step] start: 起始位置end: 结束位置step:步 跳着取,步长如果是整数,则从左往右取. 如果是负数. 则从右往左取. 默认是1 (s2[1:5:2])
(s2[:5:2])
(s2[4::2])
(s2[-5::2])
(s2[-1:-5])
(s2[-1:-5:-1])
(s2[-5::-3])
2.字符串大小转换 str.capitalize()
str.lower()
str.upper()
3.字符串去空格处理 str.strip()
str.lstrip()
str.rstrip()
4.字符串替换 str.replace(old,new)
5.字符串切割 str.split()
格式化输出几种方式: %s,%f,%d%s 用于字符串输出 ( %
%f 用于小数输出 ( % 1.2341.234000
%d. 用于整数输出 ( % 44
其他方法 (.format(,28,))
<span style="color: #0000ff">print (<span style="color: #800000">"<span style="color: #800000">我叫{0},今年{2}岁,我喜欢{1}<span style="color: #800000">".format(<span style="color: #800000">"<span style="color: #800000">周杰伦<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">周润发<span style="color: #800000">",28)) <span style="color: #008000">#<span style="color: #008000"> 指定位置<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">我叫{name},今年{age}岁,我喜欢{singer}<span style="color: #800000">".format(name=<span style="color: #800000">"<span style="color: #800000">周杰伦<span style="color: #800000">",singer=<span style="color: #800000">"<span style="color: #800000">周润发<span style="color: #800000">",age=28)) <span style="color: #008000">#<span style="color: #008000">指定关键字 6.字符串查找 str.startswith()
str.endswith())
str.count()
str.find()
str.index()
7.字符串条件判断 str.isalnum()
str.isalpha()
str.isdigit()
... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |