Python 字符串 swapcase() 方法Python 字符串方法实例将小写字母大写,大写字母小写:txt = "Hello My Name Is Elon"x = txt.swapcase()print(x)完整实例:txt = "Hello My Name
Python 字符串 strip() 方法Python 字符串方法实例删除字符串开头和结尾的空格:txt = " banana "x = txt.strip()print("of all fruits", x, "is my favorite")完整
Python 字符串 startswith() 方法Python 字符串方法实例检查字符串是否以 "Hello" 开头:txt = "Hello, welcome to my world."x = txt.startswith("Hello")print(x)完整实例:t
Python 字符串 splitlines() 方法Python 字符串方法实例将字符串拆分为一个列表,其中每一行都是一个列表项:txt = "Thank you for your visiting\nWelcome to China"x = txt.s
Python 字符串 split() 方法Python 字符串方法实例将字符串拆分成一个列表,其中每个单词都是一个列表项:txt = "welcome to China"x = txt.split()print(x)完整实例:txt = "wel
Python 字符串 rstrip() 方法Python 字符串方法实例删除字符串右边的空格:txt = " banana "x = txt.rstrip()print("of all fruits", x, "is my favorite")完整实例:t
Python 字符串 rsplit() 方法Python 字符串方法实例使用后跟空格的逗号作为分隔符,将字符串拆分为列表:txt = "apple, banana, cherry"x = txt.rsplit(", ")print(x)完整实例:t
Python 字符串 rpartition() 方法Python 字符串方法实例搜索单词 "bananas" 的最后一次出现,并返回包含三个元素的元组: 1 - “匹配”之前的所有内容 2 - “匹
Python 字符串 rjust() 方法Python 字符串方法实例返回单词 "banana" 的 20 个字符长的右对齐版本:txt = "banana"x = txt.rjust(20)print(x, "is my favorite fruit.")完整
Python 字符串 rindex() 方法Python 字符串方法实例文本中最后一次出现字符串 "China" 的位置:txt = "China is a great country. I love China."x = txt.rindex("casa")prin
Python 字符串 rfind() 方法Python 字符串方法实例文本中最后一次出现字符串 "China" 的位置:txt = "China is a great country. I love China."x = txt.rfind("casa")print(
Python 字符串 replace() 方法Python 字符串方法实例替换单词 "bananas":txt = "I like bananas"x = txt.replace("bananas", "apples")print(x)完整实例:txt = "I like banan
Python 字符串 partition() 方法Python 字符串方法实例搜索单词 "bananas",并返回包含三个元素的元组: 1 - “匹配”之前的所有内容 2 - “匹配” 3 - &l
Python 字符串 lstrip() 方法Python 字符串方法实例删除字符串左侧的空格:txt = " banana "x = txt.lstrip()print("of all fruits", x, "is my favorite")完整实例:t
Python 字符串 lower() 方法Python 字符串方法实例小写字符串:txt = "Hello my FRIENDS"x = txt.lower()print(x)完整实例:txt = "Hello my FRIENDS"x = txt.lower()print(x)