Python【字符串】【列表】【元组】常用操作(一)

简介: Python【字符串】【列表】【元组】常用操作

9.2字符串常见操作


9.2.1 find


检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1,语法:

mystr.find(str, start=0, end=len(mystr))

mystr = 'hello world! world'
print(mystr.find("ll", 0, len(mystr)))

9.2.2 index


跟find()方法一样,只不过如果str不在 mystr中会报一个异常,语法:

mystr.index(str, start=0, end=len(mystr))

mystr = 'hello world! world'
print(mystr.index("ll", 0, len(mystr)))
2

9.2.3 count


返回 str在start和end之间 在 mystr里面出现的次数,语法:

mystr.count(str, start=0, end=len(mystr))

mystr = 'hello world! world'
print(mystr.count("world", 0, len(mystr)))
2

9.2.4 replace


把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次,语法:

mystr.replace(str1, str2, mystr.count(str1))

mystr = 'hello world! world'
newStr=mystr.replace("world","Tom",mystr.count("world"))
print(newStr)
hello Tom! Tom

9.2.5 split


以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符语法:

mystr.split(str=" ", 2) '2’代表切几刀

mystr = 'hello world! world'
newStr=mystr.split(" ",2)
print(newStr)
['hello', 'world!', 'world']

9.2.6 capitalize


把字符串的第一个字符大写,其他的变成小写,语法:

mystr.capitalize()

mystr = 'hello world world'
print(mystr.capitalize())
Hello world world

9.2.7 title


把字符串的每个单词首字母大写,其他的改成小写,例:

mystr = 'hello world world'
print(mystr.title())
Hello World World

9.2.8 startswith


检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

mystr.startswith(obj)

mystr = 'hello world world'
ss='hello'
print(mystr.startswith(ss)) #大小写敏感
True

9.2.9 endswith


检查字符串是否以obj结束,如果是返回True,否则返回 False,用法同上

mystr.endswith(obj)

mystr = 'hello world world'
ss='hello'
print(mystr.endswith(ss))
False

9.2.10 lower


转换 mystr 中所有大写字符为小写

mystr.lower()

mystr = 'HELLO WORLD WORLD'
print(mystr.lower())
hello world world

9.2.11 upper


转换 mystr 中的小写字母为大写,用法同上。

mystr.upper()

mystr = 'hello world world'
print(mystr.upper())
HELLO WORLD WORLD

9.2.12 ljust


返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

mystr.ljust(width)

mystr = "hello world"
print(mystr.rjust(20,"*"))
*********hello world

9.2.13 rjust


返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

mystr.rjust(width)

mystr = "hello world world"
print(mystr.rjust(30))
             hello world world

9.2.14 center


返回一个原字符串居中,并使用空格填充至长度 width 的新字符串,用法同上。

mystr.center(width)

mystr = "hello world world"
print(mystr.center(30))
      hello world world       

9.2.15 lstrip


删除 mystr 左边的空白字符

mystr.lstrip()

mystr = "     hello world world"
print(mystr. lstrip())
hello world world

9.2.16 rstrip


删除 mystr 字符串末尾的空白字符

mystr.rstrip()

mystr = "hello world world"
print(mystr. rstrip())
hello world world    *

9.2.17 strip


删除mystr字符串两端的空白字符

mystr = "       hello world world      "
print(mystr. strip())
hello world world

9.2.18 rfind


类似于 find()函数,不过是从右边开始查找.

mystr.rfind(str, start=0,end=len(mystr) )

mystr = "hello world world"
print(mystr.rfind("h",0,len(mystr)))
0

9.2.19 rindex


类似于 index(),不过是从右边开始.

mystr.rindex( str, start=0,end=len(mystr))

mystr = "hello world world"
print(mystr.rindex("h",0,len(mystr)))
0

9.2.20 partition


把mystr以str分割成三部分,str前,str和str后

mystr.partition(str)

mystr = "helloworldworld"
print(mystr.partition("ld"))
('hellowor', 'ld', 'world')

9.2.21 rpartition


类似于 partition()函数,不过是从右边开始.

mystr.rpartition(str)

mystr = "helloworldworld"
print(mystr.rpartition("ld"))
('helloworldwor', 'ld', '')

9.2.22 splitlines


按照行分隔,返回一个包含各行作为元素的列表

mystr.splitlines()

mystr = "hello\nworld\nworld"
print(mystr.splitlines())
['hello', 'world', 'world']

9.2.23 isalpha


如果 mystr 所有字符都是字母 则返回 True,否则返回 False

mystr.isalpha()

mystr = "helloworldworld"
mystr_2 = "hello6666666"
mystr_3 = "hello world world"
print(mystr.isalpha())
print(mystr_2.isalpha())
print(mystr_3.isalpha())
True
False
False

9.2.24 isdigit


如果 mystr 只包含数字则返回 True 否则返回 False.

mystr.isdigit()

mystr = "helloworldworld"
mystr_2 = "6666666"
mystr_3 = "hello world world"
print(mystr.isdigit())
print(mystr_2.isdigit())
print(mystr_3.isdigit())
False
True
False

9.2.25 isalnum


如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False

mystr.isalnum()

mystr = "hello world world"
mystr_2 ="hello123456"
mystr_3 ="hello 123456"
print(mystr.isalnum())
print(mystr_2.isalnum())
print(mystr_3.isalnum())
False
True
False
相关文章
|
2天前
|
存储 数据处理 索引
Python列表操作的方法总结
通过掌握上述方法,你可以有效地操作Python列表,完成各种数据处理任务。列表的灵活性和多功能性使其成为Python编程中不可或缺的工具。
12 1
|
6天前
|
数据采集 算法 数据处理
Python中的列表推导式:简介与应用
【9月更文挑战第14天】本文旨在介绍Python中一种强大且简洁的构造列表的方法——列表推导式。我们将从基础语法入手,通过实例演示其用法,并探讨在数据处理和算法优化中的应用价值。文章将不包含代码示例,而是专注于概念理解和应用场景的描述,以促进读者对列表推导式的深入认识。
12 3
|
7天前
|
数据采集 数据处理 C语言
探索Python中的列表推导式
本文将深入探讨Python中强大的列表推导式功能,通过实例展示其基本语法、高级用法以及性能优势。我们将从简单的数值操作入手,逐步过渡到处理复杂数据结构,如嵌套列表和字典。此外,文章还将讨论列表推导式在提高代码可读性和减少运行时错误方面的实际价值,并通过与循环语句的对比,揭示其在特定场景下的性能优势。
|
8天前
|
数据处理 开发者 Python
探索Python中的列表推导式
在Python编程中,列表推导式(List Comprehensions)是一种简洁而强大的工具,允许开发者用一行代码生成整个列表。本文将深入探讨列表推导式的用法、优势以及在实际项目中的应用。通过具体的示例,我们将展示如何利用列表推导式简化代码,提升可读性和执行效率。无论你是编程新手还是经验丰富的开发者,都能从本文中获得有价值的见解和技巧。
|
12天前
|
C语言 Python
深入理解并实践Python中的列表推导式
深入理解并实践Python中的列表推导式
10 1
|
17天前
|
Python
在Python中,文本查找和替换的常用操作
在Python中,文本查找和替换的常用操作,使用字符串方法进行查找和替换,使用正则表达式进行查找和替换,对所查找到的内容进行计数。
17 1
|
21天前
|
Python
探索Python中的列表推导式:简洁与效率的融合
【8月更文挑战第31天】在编程的世界里,我们总是在寻找使代码更简洁、更高效的方法。Python语言提供了一种强大的工具——列表推导式,它允许我们在一行代码中生成列表,既提高了代码的可读性,也提升了执行效率。本文将带你了解列表推导式的基本概念,通过示例展示其使用场景,并探讨如何优雅地运用这一特性来简化你的Python代码。
|
4天前
|
数据处理 开发者 Python
探索Python中的列表推导式在Python编程中,列表推导式是一种简洁而高效的方法,用于从现有的列表创建新列表。本文将深入探讨列表推导式的用法、优势以及一些实际应用示例。
列表推导式是Python提供的一种强大工具,它允许开发者以更简洁的语法快速生成列表。通过结合循环和条件语句,列表推导式能够简化代码结构,提高开发效率。本文详细介绍了列表推导式的基本用法,并通过实例展示了其在数据处理、转换和过滤中的广泛应用。
10 0
|
20天前
|
数据处理 开发者 Python
探索Python中的列表推导式:一种简洁而强大的工具
【8月更文挑战第31天】在Python编程中,列表推导式是一种高效且优雅的构建列表的方法。本文将通过实际代码示例深入探讨列表推导式的不同形式及其使用场景,旨在帮助读者更好地理解和运用这一强大工具。
|
20天前
|
存储 Python
探索Python中的列表推导式
【8月更文挑战第31天】本文将深入探讨Python编程中的一个强大功能——列表推导式。我们将了解其基本概念、用法,以及如何利用这一特性简化代码和提高运行效率。通过实际的代码示例,我们会发现列表推导式不仅仅是一种语法糖,而是一种可以大幅提升代码可读性和性能的工具。