python内部给我们提供了很多字符串相关的方法,
使用方式:字符串.方法名(参数)
1. capitalize() : 将字符串的第一个字符转换为大写
如果第一个字符不是字母,就不转换


2. 字符串对齐
a. center(width, fillchar) 居中对齐
b. ljust(width, fillchar) 向左对齐
c. rjust(width, fillchar) 向右对齐
width————>长度,新的字符串的总共的长度
fillchar————> 剩下部分填充的字符。字符就是字符长度为1的字符串


例如;根据进班的顺序产生对应的学号,学号要求;


3. 字符串1.count(字符串2) ————>统计字符串1中字符串2的个数

4.字符串1.endswith(字符串)———> 判断字符串1是否以字符串2结尾

5. 字符串1.find(字符串2)-->在字符串1中查找字符串2,并返回字符2第一个字符的开始下标,如果没有就返回-1

6. isnumeric() 判断字符是否只有一个编码

7. istitle()判断标题化;每个单词首字母大写,其他字母小写

8.字符串1.join(字符串2)-->将字符串1插入到字符串2的每个字符间隔中间,产生一个新的字符串(因为字符串不可改变)

网友评论