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
目录
打赏
0
0
0
0
11
分享
相关文章
|
3月前
|
在 Python 中,如何将日期时间类型转换为字符串?
在 Python 中,如何将日期时间类型转换为字符串?
148 64
|
2月前
|
Python列表
Python列表。
55 8
[oeasy]python054_python有哪些关键字_keyword_list_列表_reserved_words
本文介绍了Python的关键字列表及其使用规则。通过回顾`hello world`示例,解释了Python中的标识符命名规则,并探讨了关键字如`if`、`for`、`in`等不能作为变量名的原因。最后,通过`import keyword`和`print(keyword.kwlist)`展示了Python的所有关键字,并总结了关键字不能用作标识符的规则。
46 9
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
16 0
python--列表list切分(超详细)
通过这些思维导图和分析说明表,您可以更直观地理解Python列表切分的概念、用法和实际应用。希望本文能帮助您更高效地使用Python进行数据处理和分析。
76 14
python--列表list切分(超详细)
通过这些思维导图和分析说明表,您可以更直观地理解Python列表切分的概念、用法和实际应用。希望本文能帮助您更高效地使用Python进行数据处理和分析。
133 10
Python 中别再用 ‘+‘ 拼接字符串了!
通过选择合适的字符串拼接方法,可以显著提升 Python 代码的效率和可读性。在实际开发中,根据具体需求和场景选择最佳的方法,避免不必要的性能损失。
62 5
|
2月前
|
使用Python计算字符串的SHA-256散列值
使用Python计算字符串的SHA-256散列值
72 7
|
3月前
|
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
62 6
Python中的列表推导式:简洁高效的数据处理
在编程世界中,效率和可读性是代码的两大支柱。Python语言以其独特的简洁性和强大的表达力,为开发者提供了众多优雅的解决方案,其中列表推导式便是一个闪耀的例子。本文将深入探讨列表推导式的使用场景、语法结构及其背后的执行逻辑,带你领略这一特性的魅力所在。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等