strint_example ="hello world good night"
index = strint_example.find("good")
print(index)12
index函数检索
strint_example ="hello world good night"
index = strint_example.index("good",0,10)#从零开始检索good是否包含在内
print(index)
Traceback (most recent call last):
File "E:/pycharm202.2.3/pythonProject1/demo1.py", line 2, in<module>
index = strint_example.index("good",0,10)
ValueError: substring not found
Process finished with exit code 1
如果包含就不报错
strint_example ="hello world good night"
index = strint_example.index("good",1,17)
print(index)12#包含运行的结果
count函数基数
strint_example ="hello world good night"
result = strint_example.count("good")
print(result)1
replace函数替换
strint_example ="hello world good night"
new_string = strint_example.replace("night","evening",1)#1代表替换次数,不能超过这个次数
print(new_string)
hello world good evening
split函数分隔符
strint_example ="hello world good night"
print(strint_example.split())
print(strint_example.split("e",1))
print(strint_example.split("h"))['hello', 'world', 'good', 'night']['h', 'llo world good night']['', 'ello world good nig', 't']
capitalize首字母大写其他小写
old_string ="hello World Good Night"
new_string = old_string.capitalize()
print(new_string)
Hello world good night
title函数标题化(所有单词都是大写开始,其余均为小写)
old_string ="hello World Good Night"
new_string = old_string.title()
print(new_string)
Hello World Good Night
startwith(检索字符窜是否是指定字符串开头)
old_string ="hello World Good Night"
new_string = old_string.startswith("hello")
print(new_string)
True
endwith检索结尾
old_string ="hello World Good Night"
new_string_one = old_string.endswith("Night")
new_string_tow = old_string.endswith("night")
print(new_string_one)
print(new_string_tow)
True
False
upper函数
old_string ="hello World Good Night"
new_string = old_string.upper()
print(new_string)
HELLO WORLD GOOD NIGHT
ljust(rjust)函数(字符串左对齐)
old_string ="hello World Good Night"
new_string = old_string.ljust(30,'-')# 宽度为30,单引号里面为剩余位置的情况
print(new_string)
hello World Good Night--------
old_string ="hello World Good Night"
new_string = old_string.rjust(30,'-')
print(new_string)
--------hello World Good Night
lstrip(rstrip)截掉左/右边空格或者指定字符
old_string_one =" hello World Good Night"
old_string_tow ="hello World Good Night "
new_string_one = old_string_one.lstrip()
new_string_tow = old_string_tow.rstrip()
print(new_string_one)
print(new_string_tow)
hello World Good Night
hello World Good Night