1. Python基础教程
  2. 在SublimeEditor中配置Python环境
  3. Python代码中添加注释
  4. Python中的变量的使用
  5. Python中的数据类型
  6. Python中的关键字
  7. Python字符串操作
  8. Python中的list操作
  9. Python中的Tuple操作
  10. Pythonmax()和min()–在列表或数组中查找最大值和最小值
  11. Python找到最大的N个(前N个)或最小的N个项目
  12. Python读写CSV文件
  13. Python中使用httplib2–HTTPGET和POST示例
  14. Python将tuple开箱为变量或参数
  15. Python开箱Tuple–太多值无法解压
  16. Pythonmultidict示例–将单个键映射到字典中的多个值
  17. PythonOrderedDict–有序字典
  18. Python字典交集–比较两个字典
  19. Python优先级队列示例

Python中,string文字是:

  • 代表Unicode字符的字节数组
  • 用单引号或双引号引起来
  • 无限长度

字符串文字

str = 'hello world'

str = "hello world"

一个多行字符串使用三个单引号或三个双引号创建的。

多行字符串文字

str = '''Say hello

to python

programming'''

str = """Say hello

to python

programming"""

Python没有字符数据类型,单个字符就是长度为1的字符串。

2. Substring or slicing

通过使用slice语法,我们可以获得一系列字符。索引从零开始。

str[m:n] 从位置2(包括)到5(不包括)返回字符串。

从索引2到5的子字符串

str = 'hello world'

print(str[2:5]) # llo

负切片将从末尾返回子字符串。

子串从索引-5到-2

str = 'hello world'

print(str[-5:-2])   # wor

str[-m:-n] 将字符串从位置-5(不包括)返回到-2(包括)。

3. Strings as arrays

在python中,字符串表现为数组。方括号可用于访问字符串的元素。

字符在第n个位置

str = 'hello world'

print(str[0])   # h

print(str[1])   # e

print(str[2])   # l

print(str[20])  # IndexError: string index out of range

4. String length

len()函数返回字符串的长度:

字符串长度

str = 'hello world'

print(len(str)) # 11

5. String Formatting

要在python中格式化s字符串,请{ }在所需位置在字符串中使用占位符。将参数传递给format()函数以使用值格式化字符串。

我们可以在占位符中传递参数位置(从零开始)。

字符串格式()

age = 36

name = 'Lokesh'

txt = "My name is {} and my age is {}"

print(txt.format(name, age))    # My name is Lokesh and my age is 36

txt = "My age is {1} and the name is {0}"

print(txt.format(name, age))    # My age is 36 and the name is Lokesh

6. String Methods

6.1. capitalize()

它返回一个字符串,其中给定字符串的第一个字符被转换为大写。当第一个字符为非字母时,它将返回相同的字符串。

字符串大写()

name = 'lokesh gupta'

print( name.capitalize() )  # Lokesh gupta

txt = '38 yrs old lokesh gupta'

print( txt.capitalize() )   # 38 yrs old lokesh gupta

6.2. casefold()

它返回一个字符串,其中所有字符均为给定字符串的小写字母。

字符串casefold()

txt = 'My Name is Lokesh Gupta'

print( txt.casefold() ) # my name is lokesh gupta

6.3. center()

使用指定的字符(默认为空格)作为填充字符,使字符串居中对齐。

在给定的示例中,输出总共需要20个字符,而“ hello world”位于其中。

字符串中心

txt = "hello world"

x = txt.center(20)

print(x)    # '    hello world     '

6.4. count()

它返回指定值出现在字符串中的次数。它有两种形式:

 count(value) - value to search for in the string.
count(value, start, end) - value to search for in the string, where search starts from start position till end position.

字符串数()

txt = "hello world"

print( txt.count("o") )         # 2

print( txt.count("o", 4, 7) )   # 1

6.5. encode()

它使用指定的编码对字符串进行编码。如果未指定编码,UTF-8将使用。

字符串encode()

txt = "My name is åmber"

x = txt.encode()

print(x)    # b'My name is \xc3\xa5mber'

6.6. endswith()

True如果字符串以指定值结尾,则返回,否则返回False。

字符串endswith()

txt = "hello world"

print( txt.endswith("world") )      # True

print( txt.endswith("planet") )     # False

6.7. expandtabs()

它将制表符大小设置为指定的空格数。

字符串expandtabs()

txt = "hello\tworld"

print( txt.expandtabs(2) )      # 'hello world'

print( txt.expandtabs(4) )      # 'hello   world'

print( txt.expandtabs(16) )     # 'hello           world'

6.8. find()

它查找指定值的第一次出现。-1如果字符串中没有指定的值,它将返回。

find()与index()方法相同,唯一的区别是,index()如果找不到该值,该方法将引发异常。

字符串find()

txt = "My name is Lokesh Gupta"

x = txt.find("e")

print(x)        # 6

6.9. format()

它格式化指定的字符串,并在字符串的占位符内插入参数值。

字符串格式()

age = 36

name = 'Lokesh'

txt = "My name is {} and my age is {}"

print( txt.format(name, age) )  # My name is Lokesh and my age is 36

6.10. format_map()

它用于返回字典键的值,以格式化带有命名占位符的字符串。

字符串format_map()

params = {'name':'Lokesh Gupta', 'age':'38'}

txt = "My name is {name} and age is {age}"

x = txt.format_map(params)

print(x)        # My name is Lokesh Gupta and age is 38

6.11. index()

  • 它在给定的字符串中查找指定值的第一次出现。
  • 如果找不到要搜索的值,则会引发异常。

字符串index()

txt = "My name is Lokesh Gupta"

x = txt.index("e")

print(x)        # 6

x = txt.index("z")  # ValueError: substring not found

6.12. isalnum()

它检查字母数字字符串。True如果所有字符都是字母数字,即字母(a-zA-Z)和数字,它将返回(0-9)。

字符串isalnum()

print("LokeshGupta".isalnum())      # True

print("Lokesh Gupta".isalnum())     # False - Contains space

6.13. isalpha()

True如果所有字符都是字母,则返回它,即字母(a-zA-Z)。

字符串isalpha()

print("LokeshGupta".isalpha())          # True

print("Lokesh Gupta".isalpha())         # False - Contains space

print("LokeshGupta38".isalpha())        # False - Contains numbers

6.14. isdecimal()

如果所有字符均为小数(0-9),则返回代码。否则返回False。

字符串isdecimal()

print("LokeshGupta".isdecimal())    # False

print("12345".isdecimal())          # True

print("123.45".isdecimal())         # False - Contains 'point'

print("1234 5678".isdecimal())      # False - Contains space

6.15. isdigit()

True如果所有字符都是数字,则返回,否则返回False。指数也被认为是数字。

字符串isdigit()

print("LokeshGupta".isdigit())      # False

print("12345".isdigit())            # True

print("123.45".isdigit())           # False - contains decimal point

print("1234\u00B2".isdigit())       # True - unicode for square 2

6.16. isidentifier()

True如果字符串是有效的标识符,则返回,否则返回False。

有效的标识符仅包含字母数字字母(a-z)和(0-9)或下划线( _ )。它不能以数字开头或包含任何空格。

字符串isidentifier()

print( "Lokesh_Gupta_38".isidentifier() )       # True

print( "38_Lokesh_Gupta".isidentifier() )       # False - Start with number

print( "_Lokesh_Gupta".isidentifier() )         # True

print( "Lokesh Gupta 38".isidentifier() )       # False - Contain spaces

6.17. islower()

True如果所有字符均小写,则返回,否则返回False。不检查数字,符号和空格,仅检查字母字符。

字符串islower()

print( "LokeshGupta".islower() )        # False

print( "lokeshgupta".islower() )        # True

print( "lokesh_gupta".islower() )       # True

print( "lokesh_gupta_38".islower() )    # True

6.18. isnumeric()

True如果所有字符都是数字(0-9),则it方法返回,否则返回False。指数也被认为是数值。

字符串isnumeric()

print("LokeshGupta".isnumeric())    # False

print("12345".isnumeric())          # True

print("123.45".isnumeric())         # False - contains decimal point

print("1234\u00B2".isnumeric())     # True - unicode for square 2

6.19. isprintable()

它返回True,如果所有字符都打印,否则返回False。不可打印字符用于指示某些格式化操作,例如:

  • 空白(被视为不可见的图形)
  • 回车
  • 标签
  • 换行
  • 分页符
  • 空字符

字符串isprintable()

print("LokeshGupta".isprintable())      # True

print("Lokesh Gupta".isprintable())     # True

print("Lokesh\tGupta".isprintable())    # False

6.20. isspace()

True如果字符串中的所有字符都是空格,则返回,否则返回False。

6.21. istitle()

它返回True如果文本的所有单词以大写字母开头,字的其余均为小写字母,即标题案例。否则False。

字符串istitle()

print("Lokesh Gupta".istitle())     # True

print("Lokesh gupta".istitle())     # False

6.22. isupper()

True如果所有字符均大写,则返回,否则返回False。不检查数字,符号和空格,仅检查字母字符。

字符串isupper()

print("LOKESHGUPTA".isupper())      # True

print("LOKESH GUPTA".isupper())     # True

print("Lokesh Gupta".isupper())     # False

6.23. join()

它以可迭代方式获取所有项目,并使用强制性指定的分隔符将它们连接为一个字符串。

字符串join()

myTuple = ("Lokesh", "Gupta", "38")

x = "#".join(myTuple)

print(x)    # Lokesh#Gupta#38

6.24. ljust()

此方法将使用指定的字符(默认为空格)作为填充字符使字符串左对齐。

字符串ljust()

txt = "lokesh"

x = txt.ljust(20, "-")

print(x)    # lokesh--------------

6.25. lower()

它返回一个字符串,其中所有字符均为小写。符号和数字将被忽略。

字符串lower()

txt = "Lokesh Gupta"

x = txt.lower()

print(x)    # lokesh gupta

6.26. lstrip()

它删除所有前导字符(默认为空格)。

字符串lstrip()

txt = "#Lokesh Gupta"

x = txt.lstrip("#_,.")

print(x)    # Lokesh Gupta

6.27. maketrans()

它创建一个字符到其转换/替换的一对一映射。当在translate()方法中使用时,此翻译映射随后用于将字符替换为其映射的字符。

字符串maketrans()

dict = {"a": "123", "b": "456", "c": "789"}

string = "abc"

print(string.maketrans(dict))   # {97: '123', 98: '456', 99: '789'}

6.28. partition()

它在给定的文本中搜索指定的字符串,并将该字符串拆分为包含三个元素的元组:

  • 第一个元素包含指定字符串之前的部分。
  • 第二个元素包含指定的字符串。
  • 第三个元素包含字符串后面的部分。

字符串partition()

txt = "my name is lokesh gupta"

x = txt.partition("lokesh")

print(x)    # ('my name is ', 'lokesh', ' gupta')

print(x[0]) # my name is

print(x[1]) # lokesh

print(x[2]) #  gupta

6.29. replace()

它将指定的短语替换为另一个指定的短语。它有两种形式:

  • string.replace(oldvalue, newvalue)
  • string.replace(oldvalue, newvalue, count)–“计数”指定要替换的出现次数。默认为所有事件。

字符串replace()

txt = "A A A A A"

x = txt.replace("A", "B")

print(x)    # B B B B B

x = txt.replace("A", "B", 2)

print(x)    # B B A A A

6.30. rfind()

它查找指定值的最后一次出现。-1如果在给定的文本中找不到该值,则返回该值。

字符串rfind()

txt = "my name is lokesh gupta"

x = txt.rfind("lokesh")    

print(x)        # 11

x = txt.rfind("amit")      

print(x)        # -1

6.31. rindex()

它查找指定值的最后一次出现,如果找不到该值,则引发异常。

字符串rindex()

txt = "my name is lokesh gupta"

x = txt.rindex("lokesh")       

print(x)                # 11

x = txt.rindex("amit")  # ValueError: substring not found

6.32. rjust()

它将使用指定的字符(默认为空格)作为填充字符来右对齐字符串。

字符串rjust()

txt = "lokesh"

x = txt.rjust(20,"#")

print(x, "is my name")  # ##############lokesh is my name

6.33. rpartition()

它搜索指定字符串的最后一次出现,并将该字符串拆分为包含三个元素的元组。

  • 第一个元素包含指定字符串之前的部分。
  • 第二个元素包含指定的字符串。
  • 第三个元素包含字符串后面的部分。

字符串rpartition()

txt = "my name is lokesh gupta"

x = txt.rpartition("lokesh")

print(x)    # ('my name is ', 'lokesh', ' gupta')

print(x[0]) # my name is

print(x[1]) # lokesh

print(x[2]) #  gupta

6.34. rsplit()

它将字符串从右开始拆分为一个列表。

字符串rsplit()

txt = "apple, banana, cherry"

x = txt.rsplit(", ")

print(x)    # ['apple', 'banana', 'cherry']

6.35. rstrip()

它删除所有结尾字符(字符串末尾的字符),空格是默认的结尾字符。

字符串rstrip()

txt = "     lokesh     "

x = txt.rstrip()

print(x)    # '     lokesh'

6.36. split()

它将字符串拆分为列表。您可以指定分隔符。默认分隔符为空格。

字符串split()

txt = "my name is lokesh"

x = txt.split()

print(x)    # ['my', 'name', 'is', 'lokesh']

6.37. splitlines()

通过在换行符处进行拆分,它将字符串拆分为列表。

字符串splitlines()

txt = "my name\nis lokesh"

x = txt.splitlines()

print(x)    # ['my name', 'is lokesh']

6.38. startswith()

True如果字符串以指定值开头,则返回,否则返回False。字符串比较区分大小写。

字符串startswith()

txt = "my name is lokesh"

print( txt.startswith("my") )   # True

print( txt.startswith("My") )   # False

6.39. strip()

它将删除所有前导(开头的空格)和结尾(结尾的空格)字符(默认为空格)。

字符串strip()

txt = "   my name is lokesh   "

print( txt.strip() )    # 'my name is lokesh'

6.40. swapcase()

它返回一个字符串,其中所有大写字母均为小写字母,反之亦然。

字符串swapcase()

txt = "My Name Is Lokesh Gupta"

print( txt.swapcase() ) # mY nAME iS lOKESH gUPTA

6.41. title()

它返回一个字符串,其中每个单词的第一个字符均为大写。如果单词开头包含数字或符号,则其后的第一个字母将转换为大写字母。

字符串标题()

print( "lokesh gupta".title() ) # Lokesh Gupta

print( "38lokesh gupta".title() )   # 38Lokesh Gupta

print( "1. lokesh gupta".title() )  # Lokesh Gupta

6.42. translate()

它需要转换表来根据映射表替换/翻译给定字符串中的字符。

字符串translate()

translation = {97: None, 98: None, 99: 105}

string = "abcdef"  

print( string.translate(translation) )  # idef

6.43. upper()

它返回一个字符串,其中所有字符均为大写。符号和数字将被忽略。

字符串upper()

txt = "lokesh gupta"

print( txt.upper() )    # LOKESH GUPTA

6.44. zfill()

它在字符串的开头添加零(0),直到达到指定的长度。

字符串zfill()

txt = "100"

x = txt.zfill(10)

print( 0000000100 ) # 0000000100

学习愉快!

(Python基础教程之七)Python字符串操作的更多相关文章

  1. python基础教程总结2——字符串

    1.基本操作 序列操作:索引,分片,乘法,判断成员资格,长度,最值...... 注:字符串不可变,分片赋值不合法 2.字符串格式化 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格 ...

  2. Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级

    前文5.2节和5.4节分别就Python下读文件操作做了基础性讲述和提升性介绍,但是仍有些问题,比如在5.4节里涉及到一个多次读文件的问题,实际上我们还没有完全阐述完毕,下面这个图片的问题在哪呢? 问 ...

  3. 《python基础教程(第二版)》学习笔记 字符串(第3章)

    <python基础教程(第二版)>学习笔记 字符串(第3章)所有的基本的序列操作(索引,分片,乘法,判断成员资格,求长度,求最大最小值)对字符串也适用.字符串是不可以改变的:格式化输出字符 ...

  4. (Python基础教程之八)Python中的list操作

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  5. .Net程序员之Python基础教程学习----列表和元组 [First Day]

    一. 通用序列操作: 其实对于列表,元组 都属于序列化数据,可以通过下表来访问的.下面就来看看序列的基本操作吧. 1.1 索引: 序列中的所有元素的下标是从0开始递增的. 如果索引的长度的是N,那么所 ...

  6. python基础教程笔记—即时标记(详解)

    最近一直在学习python,语法部分差不多看完了,想写一写python基础教程后面的第一个项目.因为我在网上看到的别人的博客讲解都并不是特别详细,仅仅是贴一下代码,书上内容照搬一下,对于当时刚学习py ...

  7. python学习笔记(二)、字符串操作

    该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...

  8. 《python基础教程(第二版)》学习笔记 列表/元组(第2章)

    <python基础教程(第二版)>学习笔记 列表/元组(第2章)序列中的下标从0开始x='ABC' ==> x[0]='A', x[1]='B', x[2]='C'负数索引从右边开始 ...

  9. Python基础教程总结(一)

    引言: 一直都听说Python很强大,以前只是浏览了一些博客,发现有点像数学建模时使用的Matlab,就没有深入去了解了.如今Python使用的地方越来越多,最近又在学习机器学习方面的知识,因此想系统 ...

  10. Python基础教程(第2版 修订版) pdf

    Python基础教程(第2版 修订版) 目录 D11章快速改造:基础知识11.1安装Python11.1.1Windows11.1.2Linux和UNIX31.1.3苹果机(Macintosh)41. ...

随机推荐

  1. LOTO示波器统计曲线和故障分析pass/fail测试

    LOTO示波器统计曲线和故障分析pass/fail测试 虚拟示波器可以应用在工业自动化检测中,除了常规的检测波形和测量值参数以外,由多个行业客户定制和验证的统计曲线和故障分析(pass/fail)功能 ...

  2. Angular Material 18+ 高级教程 – CDK Overlay

    Overlay, Dialog, Modal, Popover 傻傻分不清楚 参考: Medium – Modal?Dialog?你真的知道他們是什麼嗎? Popups, dialogs, toolt ...

  3. x64汇编——汇编指令

    汇编指令 mov dest, src mov move的简称 将src的内容赋值给dest,类似于dest = src [地址值] 中扩号 [ ]里面放的都是内存地址 一个变量的地址值,是它所有字节地 ...

  4. 多表查询 —— 内连接&外连接&子查询

    连接查询 内连接 1.查询语法 -- 隐式内连接 select 字段列表 from 表1, 表2... where 条件; -- 显式内连接 select 字段列表 from 表1 [INNER] j ...

  5. Flutter 这一年:2022 亮点时刻

    回看 2022,展望 Flutter Forward 2022 年,我们非常兴奋的看到 Flutter 社区持续发展壮大,也因此让更多人体验到了令人难以置信的体验.每天有超过 1000 款使用 Flu ...

  6. SaaS业务架构:业务能力分析

    大家好,我是汤师爷~ 今天聊聊SaaS业务架构的业务能力分析. 业务能力概述 简单来说,业务能力是企业"做某事的能力". 业务能力描述了企业当前和未来应对挑战的能力,即企业能做什么 ...

  7. NIO实现聊天室之:一切都要从网络编程的基础开始聊起!

    一.写在开头 大家好,Build哥回来啦!停更了大概2个月之久,之前有段时间去写小说去了,后来又因为公司活太多,牛马干的太投入,就拉下了博客的更新,国庆节期间,难得的闲下来,准备回归老本行啦. 大致的 ...

  8. mysql-存储过程(1) mysql循环语句

    mysql循环语句: 本文总结了mysql常见的三种循环方式:while.repeat和loop循环.还有一种goto,不推荐使用. 一.while循环 delimiter // #定义标识符为双斜杠 ...

  9. document.designMode 设计模式

    document.designMode 的默认值是 off 关闭: 在控制台输入 document.designMode = 'on' 就可以编辑页面内容:

  10. 创建一个简单的基于MyBatis的项目