Flask基础知识--字符串常用操作
假设有语句:mystr = 'hello world !love or like the world'
1.find. 检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1
语法:mystr.find(str, start=0, end=len(mystr))
> mystr = 'hello world !love or like the world'> mystr.find("world", 0, -1)6
2.index. 跟find()方法一样,只不过如果str不在 mystr中会报一个异常.
语法:
mystr.index(str, start=0, end=len(mystr))
> mystr = 'hello world !love or like the world'> mystr.index("love",0 ,30)13
3. count. 返回 str在start和end之间 在 mystr里面出现的次数
语法:
mystr.count(str, start=0, end=len(mystr))
> mystr = 'hello world !love or like the world'> mystr.count("world")2
4.replace.
把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.
语法:
mystr.replace(str1, str2, mystr.count(str1))
> mystr = 'hello world !love or like the world'> mystr.replace("world", "baby",2)'hello baby !love or like the baby'
5.split.
以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串。
语法:mystr.split(str, maxsplit)
> mystr = 'hello world !love or like the world'> mystr.split("world",2)['hello ', ' !love or like the ', '']
6. capitalize. 把字符串的第一个字符大写
语法:mystr.capitalize()
> mystr = 'hello world !love or like the world'> mystr.capitalize()'Hello world !love or like the world'
7.title. 把字符串的每个单词首字母大写
语法:mystr.title()
> mystr = 'hello world !love or like the world'> mystr.title()'Hello World !Love Or Like The World'
8.startswith().
检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False
语法:mystr.startswith(obj)
> mystr = 'hello world !love or like the world'> mystr.startswith("hello")True> mystr.startswith("Hello")False
9.endswith(). 检查字符串是否以obj结束,如果是返回True,否则返回 False.
语法:mystr.endswith(obj)
> mystr = 'hello world !love or like the world'> mystr.endswith("rld")True
10.lower. 转换 mystr 中所有大写字符为小写
语法:mystr.lower()
> mystr = "HELLO WORLD !LOVE OR LIKE THE WORLD"> mystr.lower()'hello world !love or like the world'
11.upper. 转换 mystr 中的小写字母为大写
语法:mystr.upper()
> mystr = 'hello world !love or like the world'> mystr.upper()'HELLO WORLD !LOVE OR LIKE THE WORLD'
12.ljust. 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
语法:mystr.ljust(width)
> mystr = "hello world !love or like the world"> mystr.ljust(40)'hello world !love or like the world '
13.rjust. 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
语法:mystr.rjust(width)
> mystr = "hello world !love or like the world"> mystr.rjust(40)' hello world !love or like the world'
14.center. 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
语法:mystr.center(width)
> mystr = "hello world !love or like the world"> mystr.center(40)' hello world !love or like the world '
15.lstrip. 删除 mystr 左边的空白字符
语法:mystr.lstrip()
> mystr = " hello world !love or like the world"> mystr.lstrip()'hello world !love or like the world'
16.rstrip. 删除 mystr 右边的空白字符
语法:mystr.rstrip()
> mystr = "hello world !love or like the world "> mystr.rstrip()'hello world !love or like the world'
17.strip. 删除mystr字符串两端的空白字符
语法:mystr.strip()
> mystr = " hello world !love or like the world "> mystr.strip()'hello world !love or like the world'
18.rfind. 类似于 find()函数,不过是从右边开始查找.
语法:mystr.rfind()
> mystr = 'hello world !love or like the world'> mystr.rfind('world')30
19.rindex. 类似于 index(),不过是从右边开始.
语法:mystr.rindex()
> mystr = 'hello world !love or like the world'> mystr.rindex("and")Traceback (most recent call last):File "<pyshell#33>", line 1, in <module>mystr.rindex("and")ValueError: substring not found
20.partition. 把mystr以str分割成三部分,str前,str和str后
语法:mystr.partition(str)
> mystr = 'hello world !love or like the world'> mystr.partition("world")('hello ', 'world', ' !love or like the world')
21.rpartition. 类似于 partition()函数,不过是从右边开始.
语法:mystr.rpartition()
> mystr = 'hello world !love or like the world'> mystr.rpartition("world")('hello world !love or like the ', 'world', '')
22.splitlines. 按照行分隔,返回一个包含各行作为元素的列表
语法:mystr.splitlines()
> mystr = "hello world \n love or \n like world"> mystr.splitlines()['hello world ', ' love or ', ' like world']
23.isalpha. 如果 mystr 所有字符都是字母 则返回 True,否则返回 False
语法:mystr.isalpha()
> mystr = 'hello world love or like the world'> mystr.isalpha()False> mystr = "hello"> mystr.isalpha()True
24.isdigit. 如果 mystr 只包含数字则返回 True 否则返回 False.
语法:mystr.isdigit()
> mystr = "12345"> mystr.isdigit()True
25.isalnum.如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False
语法:mystr.isalnum()
> mystr = "1234hello"> mystr.isalnum()True
26.isspace. 如果 mystr 中只包含空格,则返回 True,否则返回 False.
语法:mystr.isspace()
> mystr = " haha"> mystr.isspace()False
27.join. mystr 中每个字符后面插入str,构造出一个新的字符串
语法:mystr.join(str)
> mystr = "_"> li = ["hello","world","love","you"]> mystr.join(li)'hello_world_love_you'
Q: 给定一个字符串mystr,返回使用空格或者'\t'分割后的倒数第二个子串
mystr = "helle world \t love you \t and the world \t hei hei"
