Python字符串内建函数
1、find函数:检测字符串是否包括子字符串
str.find(sub, [start,end])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.find("hello")) #打印hello第一次出现的下标
print(str_example.find("lll")) #如果没有出现过 则打印-1
打印结果:
0
1
2、index函数:检测字符串是否包括子字符串
str.index (sub, [start,end])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.index("hello")) #打印hello第一次出现的下标
print(str_example.index("no")) #如果字符串中没有 报异常
打印结果:
0
报异常
3、count函数∶统计字符串中某个字符的个数
str.count(sub, [start,end])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.count("o"))
打印结果:
3
4、replace函数:将旧字符串替换为新字符串
str.replace(old, new,[count])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.replace("hello","nohello"))
打印结果:
nohello world I am jinjiaodawang
5、splite函数︰通过指定分隔符对字符串进行切片
str.split(str="", num=string.count(str))
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.split(" ",2)) #以空格为分隔符 实行两次
打印结果:
['hello', 'world', 'I am jinjiaodawang']
6、capitalize :第一个字符大写,其他字符小写
str.capitalize()
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.capitalize())
打印结果:
Hello world i am jinjiaodawang
7、title :所有单词首字母大写,其余字母小写
str.title()
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.title())
打印结果:
Hello World I Am Jinjiaodawang
8、startswith :检查字符串是否以制定子串开头
str.startswith(str, beg=0,end=len(string))
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.startswith("hello"))
print(str_example.startswith("lloheoo"))
打印结果:
True
False
9、endswith :检查字符串是否以制定子串结尾
str.endswith(str, beg=0,end=len(string))
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.endswith("wang"))
print(str_example.endswith("llo"))
打印结果:
True
False
10、upper:将小写字母转为大写字母
str.upper()
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.upper())
打印结果:
HELLO WORLD I AM JINJIAODAWANG
11、lower :将小写字母转为大写字母
str.lower()
示例代码:
str_example = "HELLO world I am jinjiaodawang"
print(str_example.lower())
打印结果:
hello world i am jinjiaodawang
12、ljust:左对齐,使用空格填充至指定长度的新字符串
str.ljust(width,[fillchar])
示例代码:
str_example = "HELLO world I am jinjiaodawang"
print(str_example.ljust(50,"&"))
打印结果:
HELLO world I am jinjiaodawang&&&&&&&&&&&&&&&&&&&&
13、rjust :右对齐,使用空格填充至指定长度的新字符串
str.rjust(width,[fillchar])
示例代码:
str_example = "HELLO world I am jinjiaodawang"
print(str_example.rjust(50,"&"))
打印结果:
&&&&&&&&&&&&&&&&&&&&HELLO world I am jinjiaodawang
14、center :返回一个指定的宽度width居中的字符串
str.center(width,[fillchar])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.center(50,"-"))
打印结果:
----------HELLO world I am jinjiaodawang----------
15、lstrip:截掉字符串左边的空格或指定字符;
rstrip:截掉字符串右边的空格或指定字符;
strip:截掉字符串左右两边的空格或指定字符
str.lstrip([chars])
str.rstrip([chars])
str.strip([chars])
示例代码:
str_example = "++hello world I am jinjiaodawang++"
print(str_example.lstrip("+"))
print(str_example.rstrip("+"))
print(str_example.strip("+"))
打印结果:
hello world I am jinjiaodawang++
++hello world I am jinjiaodawang
hello world I am jinjiaodawang