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">我叫{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">指定关键字
<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()
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。