*程序员从0开始数*
切片字符:
[起始索引:结束索引(取不到)]
[起始索引:结束索引:步长]
反序:[::-1]
mystr="1234abcd"
mystr[1:5]
mystr[:5]
mystr[:]
mystr[1:]
mystr[-2:]
mystr[1:5:2]
mystr[-1::-1]
mystr[::-1]
in/not in:判断一个字符串在另一个字符串中是否存在
substr="abc"
if substr in mystr:
print("存在")
else:
print("不存在")
特殊符号输出原样输出:\(转译)或r""(r注意引号)
常用字符串格式化
%s 字符占位
%d 数字占位
%f 浮点数
"{},{}".format(,)
想输出一个{},就输入两个{{}} *只能输出大括号.format()不能替换*
字符串转换
mystr="hello"
len(mystr)
mystr.count("...") mystr中有几个...
mystr.capitalize("...")字符串首字母大写
mystr.title("...") 将每一个单词首字母大写
mystr.find("...") 找到...返回下标,不存在的返回-1 *等同index()不存在会产生异常
mystr.rfind("...") 从右边寻找 *等同rindex()不存在会产生异常
mystr.replace("...","...")替换
mystr.split("")用""将每个字符引起,split()是通过空格或\t进行拆分
mystr.join("...")加入...组成字符串
ljust(),rjust(),center()左右中对齐输出,()中先大小后填充
strip(),lstrip(),rstrip()去除空格
mystr.partition("xxx")以xxx拆分
isalpha():判断字符串是否是字母
isdigit():判断字符串是否是数字
isalnum():判断字符串是否是数字和字母
splitlines()换行符拆分
startswith():以XX开头
endswith():以XX结尾
lower():大写转小写
upper():小写转大写
切片字符:
[起始索引:结束索引(取不到)]
[起始索引:结束索引:步长]
反序:[::-1]
mystr="1234abcd"
mystr[1:5]
mystr[:5]
mystr[:]
mystr[1:]
mystr[-2:]
mystr[1:5:2]
mystr[-1::-1]
mystr[::-1]
in/not in:判断一个字符串在另一个字符串中是否存在
substr="abc"
if substr in mystr:
print("存在")
else:
print("不存在")
特殊符号输出原样输出:\(转译)或r""(r注意引号)
常用字符串格式化
%s 字符占位
%d 数字占位
%f 浮点数
"{},{}".format(,)
想输出一个{},就输入两个{{}} *只能输出大括号.format()不能替换*
字符串转换
mystr="hello"
len(mystr)
mystr.count("...") mystr中有几个...
mystr.capitalize("...")字符串首字母大写
mystr.title("...") 将每一个单词首字母大写
mystr.find("...") 找到...返回下标,不存在的返回-1 *等同index()不存在会产生异常
mystr.rfind("...") 从右边寻找 *等同rindex()不存在会产生异常
mystr.replace("...","...")替换
mystr.split("")用""将每个字符引起,split()是通过空格或\t进行拆分
mystr.join("...")加入...组成字符串
ljust(),rjust(),center()左右中对齐输出,()中先大小后填充
strip(),lstrip(),rstrip()去除空格
mystr.partition("xxx")以xxx拆分
isalpha():判断字符串是否是字母
isdigit():判断字符串是否是数字
isalnum():判断字符串是否是数字和字母
splitlines()换行符拆分
startswith():以XX开头
endswith():以XX结尾
lower():大写转小写
upper():小写转大写