Python中的字符串方法

Python中的字符串方法

字符串类即str提供了许多有用的方法来操纵字符串。具体来说,我们将讨论如下的方法。

  1. 搜索字符串内的子字符串。
  2. 测试字符串。
  3. 格式字符串。
  4. 转换字符串。

回顾前面的章节,方法是属于某个对象的功能。然而,与函数不同,一个方法总是使用下面的符号在对象上调用。

object.method_name(arg1, arg2, arg3, ...., argN)

好吧,我们开始吧。

测试字符串

str该类的以下方法测试字符串内的各种类型的字符。

方法

描述

str.isalnum()

返回True如果字符串中的所有字符的字母数字是(其中包含任一数目或字母或两者的字符串)。否则False。

str.isalpha()

True如果字符串中的所有字符均为字母,则返回。否则False。

str.isdigit()

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

str.islower()

返回True如果字符串中的所有字符为小写。否则False。

str.isupper()

返回True如果字符串中的所有字符都是大写的。否则False。

str.isspace()

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

这里有些例子:

isalnum()方法

>>> 

>>> s = "A bite of python"

>>> s.isalnum() 

False

>>> 

>>> "123".isalnum()

True

>>> "abc".isalnum()

True

>>> "abc123".isalnum()

True

>>> 

isalpha()方法

>>> 

>>> "123".isalpha()

False

>>> 

>>> "zyz".isalpha()

True

>>> 

>>> "$$$$".isalpha()

False

>>> 

>>> "abc233".isalpha()

False

>>> 

isdigit()方法

>>> 

>>> 

>>> "name101".isdigit()

False

>>> 

>>> "101".isdigit()

True

>>> 

>>> "101 ".isdigit()

False

>>> 

>>> "101.129".isdigit()

False

>>> 

````

 

## islower() and isupper() method

 

```python

>>> 

>>> s

'A bite of python'

>>> 

>>> s.islower()

False

>>> 

>>> "abc".islower()

True

>>> 

>>> s.isupper()

False

>>> 

>>> 

>>> "ABC".isupper()

True

>>> 

isspace()方法

>>> 

>>> "\n\t".isspace()

True

>>> 

>>> " \n\t".isspace()

True

>>> 

>>> "@ \n\t".isspace()

False

>>> "123".isspace()

False

搜索和替换字符串

本str类有以下方法,它允许你搜索字符串里子。

方法

描述

endswith(sub)

True如果字符串以子串结尾,则返回sub。否则False。

startswith(sub)

True如果字符串以substring开头则返回sub。否则False。

find(sub)

返回sub找到子字符串的最低索引。如果sub未找到子字符串-1则返回。

rfind(sub)

返回sub找到子字符串的最高索引。如果sub未找到子字符串-1则返回。

count(sub)

它返回在字符串中sub找到的子串的出现次数。如果没有找到事件0返回。

replace(old, new)

它用old子字符串替换后返回一个新的字符串new。请注意,它不会更改其被调用的对象。

一些例子:

>>> 

>>> s = "abc"

>>> s.endswith("bc")

True

>>> 

>>> "python".startswith("py")

True

>>> 

>>> "Learning Python".find("n")

4

>>> 

>>> "Learning Python".rfind("n")

14

>>> 

>>> "Learning Python".find("at")

-1

>>> 

>>> 

>>> "procrastination is the thief of time".count("ti")

3

>>> 

>>> 

>>> 

>>> s1 = "Learning C"  # old string

>>> id(s1)

49447664   # id of s1

>>> 

>>> s2 = s.replace("C", "Python")   # replace() creates a new string and assigns it to s2

>>> s2 

'Learning Python'

>>> 

>>> id(s1)

49447664   # notice that s1 object is not changed at all

>>> 

>>> 

转换字符串

以下方法通常用于返回字符串的修改版本。

方法

描述

lower()

将所有字符转换为小写后返回字符串的新副本。

upper()

将所有字符转换为大写字符后,返回字符串的新副本。

capitalize()

在大写仅字符串中的第一个字母后返回字符串的新副本。

title()

在每个单词的首字母大写后返回字符串的新副本。

swapcase()

将小写字母转换为大写字母,反之亦然后返回新的副本。

strip()

删除所有前导和尾随空白字符后,返回字符串的新副本。

strip(chars)

chars从字符串的开头和结尾删除后返回字符串的新副本。

一定要记住这些方法返回一个新的字符串,不要修改以任何方式被调用的对象。

这里有些例子:

lower()方法

>>> 

>>> "abcDEF".lower()

'abcdef'

>>> 

>>> "abc".lower()

'abc'

>>> 

upper()方法

>>> 

>>> "ABCdef".upper()

'ABCDEF'

>>> 

>>> "ABC".upper()

'ABC'

>>> 

大写()和标题()方法

>>> 

>>> "a long string".capitalize()

'A long string'

>>> 

>>> 

>>> "a long string".title()

'A Long String'

>>> 

>>> 

swapcase()方法

>>> 

>>> "ABCdef".swapcase()

'abcDEF'

>>> 

>>> "def".swapcase()

'DEF'

>>> 

strip()方法

>>> 

>>> s1 = "\n\tName\tAge"

>>> print(s1)

 

        Name    Age

>>> 

>>> 

>>> s2 = s1.strip()

>>> s2

'Name\tAge'

>>> print(s2)

Name    Age

>>> 

>>> 

>>> s  = "--Name\tAge--"

>>> 

>>> s.strip("-")     # return a new copy of string after removing - characters from beginning and end of the string

'Name\tAge'

>>> 

>>> 

格式化方法

下表列出了str该类的一些格式化方法。

方法

描述

center(width)

在将其居中放置在长度宽度字段中之后,返回字符串的新副本。

ljust(width)

返回长度宽度字段中左对齐字符串的新副本。

rjust(width)

在长度宽度字段中返回右对齐字符串的新副本。

center()方法

>>> 

>>> "NAME".center(20)

'        NAME        '

>>> 

>>> 

ljust()方法

>>> 

>>> "NAME".ljust(10)

'NAME      '

>>> "NAME".ljust(4)

'NAME'

>>> "NAME".ljust(5)

'NAME '

>>> 

rjust()方法

>>> 

>>> "NAME".rjust(10)

'      NAME'

>>> 

>>> "NAME".rjust(4)

'NAME'

>>> 

>>> "NAME".rjust(5)

' NAME'

>>> 

>>> 

猜你喜欢

转载自www.cnblogs.com/dengpeiyou/p/9209963.html