前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【三】Python基础之数据类型:字符串

【三】Python基础之数据类型:字符串

作者头像
菲宇
发布2019-06-12 16:41:19
4960
发布2019-06-12 16:41:19
举报
文章被收录于专栏:菲宇菲宇

字符串

字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。字符串常用功能:移除空白、分割、长度、索引、切片。

Python转义字符

在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符。如下表:

转义字符

描述

\(在行尾时)

续行符

\\

反斜杠符号

\'

单引号

\"

双引号

\a

响铃

\b

退格(Backspace)

\e

转义

\000

\n

换行

\v

纵向制表符

\t

横向制表符

\r

回车

\f

换页

\oyy

八进制数,yy代表的字符,例如:\o12代表换行

\xyy

十六进制数,yy代表的字符,例如:\x0a代表换行

\other

其它的字符以普通格式输出


Python字符串运算符

下表实例变量a值为字符串 "Hello",b变量值为 "Python":

操作符

描述

实例

+

字符串连接

a + b 输出结果: HelloPython

*

重复输出字符串

a*2 输出结果:HelloHello

[]

通过索引获取字符串中字符

a[1] 输出结果 e

[ : ]

截取字符串中的一部分,遵循左闭右开原则,str[0,2] 是不包含第 3 个字符的。

a[1:4] 输出结果 ell

in

成员运算符 - 如果字符串中包含给定的字符返回 True

'H' in a 输出结果 True

not in

成员运算符 - 如果字符串中不包含给定的字符返回 True

'M' not in a 输出结果 True

r/R

原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母 r(可以大小写)以外,与普通字符串有着几乎完全相同的语法。

print( r'\n' ) print( R'\n' )

%

格式字符串

请看下边内容。

Python字符串格式化

Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。

python字符串格式化符号:

符 号

描述

%c

格式化字符及其ASCII码

%s

格式化字符串

%d

格式化整数

%u

格式化无符号整型

%o

格式化无符号八进制数

%x

格式化无符号十六进制数

%X

格式化无符号十六进制数(大写)

%f

格式化浮点数字,可指定小数点后的精度

%e

用科学计数法格式化浮点数

%E

作用同%e,用科学计数法格式化浮点数

%g

%f和%e的简写

%G

%f 和 %E 的简写

%p

用十六进制数格式化变量的地址

格式化操作符辅助指令:

符号

功能

*

定义宽度或者小数点精度

-

用做左对齐

+

在正数前面显示加号( + )

<sp>

在正数前面显示空格

#

在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于用的是'x'还是'X')

0

显示的数字前面填充'0'而不是默认的空格

%

'%%'输出一个单一的'%'

(var)

映射变量(字典参数)

m.n.

m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)

1、百分号方式

%[(name)][flags][width].[precision]typecode

  • (name) 可选,用于选择指定的key
  • flags 可选,可供选择的值有:
    • + 右对齐;正数前加正好,负数前加负号;
    • - 左对齐;正数前无符号,负数前加负号;
    • 空格 右对齐;正数前加空格,负数前加负号;
    • 0 右对齐;正数前无符号,负数前加负号;用0填充空白处
  • width 可选,占有宽度
  • .precision 可选,小数点后保留的位数
  • typecode 必选
    • s,获取传入对象的__str__方法的返回值,并将其格式化到指定位置
    • r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
    • c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
    • o,将整数转换成 八 进制表示,并将其格式化到指定位置
    • x,将整数转换成十六进制表示,并将其格式化到指定位置
    • d,将整数、浮点数转换成 十 进制表示,并将其格式化到指定位置
    • e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
    • E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
    • f, 将整数、浮点数转换成浮点数表示,并将其格式化到指定位置(默认保留小数点后6位)
    • F,同上
    • g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
    • G,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
    • %,当字符串中存在格式化标志时,需要用 %%表示一个百分号

注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式

常用格式化:

1 2 3 4 5 6 7 8 9 10 11

tpl = "i am %s" % "alex" tpl = "i am %s age %d" % ("alex", 18) tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} tpl = "percent %.2f" % 99.97623 tpl = "i am %(pp).2f" % {"pp": 123.425556, } tpl = "i am %.2f %%" % {"pp": 123.425556, }

2、Format方式

[[fill]align][sign][#][0][width][,][.precision][type]

  • fill 【可选】空白处填充的字符
  • align 【可选】对齐方式(需配合width使用)
    • <,内容左对齐
    • >,内容右对齐(默认)
    • =,内容右对齐,将符号放置在填充字符的左侧,且只对数字类型有效。 即使:符号+填充物+数字
    • ^,内容居中
  • sign 【可选】有无符号数字
    • +,正号加正,负号加负;
    • -,正号不变,负号加负;
    • 空格 ,正号空格,负号加负;
  • # 【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示
  • , 【可选】为数字添加分隔符,如:1,000,000
  • width 【可选】格式化位所占宽度
  • .precision 【可选】小数位保留精度
  • type 【可选】格式化类型
    • 传入” 字符串类型 “的参数
      • s,格式化字符串类型数据
      • 空白,未指定类型,则默认是None,同s
    • 传入“ 整数类型 ”的参数
      • b,将10进制整数自动转换成2进制表示然后格式化
      • c,将10进制整数自动转换为其对应的unicode字符
      • d,十进制整数
      • o,将10进制整数自动转换成8进制表示然后格式化;
      • x,将10进制整数自动转换成16进制表示然后格式化(小写x)
      • X,将10进制整数自动转换成16进制表示然后格式化(大写X)
    • 传入“ 浮点型或小数类型 ”的参数
      • e, 转换为科学计数法(小写e)表示,然后格式化;
      • E, 转换为科学计数法(大写E)表示,然后格式化;
      • f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
      • F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
      • g, 自动在e和f中切换
      • G, 自动在E和F中切换
      • %,显示百分比(默认显示小数点后6位)

常用格式化:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

tpl = "i am {}, age {}, {}".format("seven", 18, 'alex') tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) tpl = "i am {0}, age {1}, really {0}".format("seven", 18) tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tpl = "i am {:s}, age {:d}".format(*["seven", 18]) tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

更多格式化操作:https://docs.python.org/3/library/string.html

1.字符串连接

1 2 3 4 5 6 7 8 9

>>> a = 'My name is ' + 'Suen' >>> a 'My name is Suen' >>> a = 'My name is %s'%'Suen' >>> a 'My name is Suen' >>> a = 'My name is %s, Age:%d'%('Suen', 18) >>> a 'My name is Suen, Age:18'<br>>>> t = "i am %(name)s , age %(age)d" % {"name": "suen", "age": 18} # %(name)s 获取字典的name %(age)d 获取字典的age<br>>>> t<br>>>> 'i am suen , age 18'<br>>>> tp = "i am %(pp).2f " %{"pp": 23.45789}<br>>>> tp<br>'i am 23.46'

2.打印浮点数

1 2 3 4 5 6

>>> str1 = '浮点数:%.2f' % 199.1568 # %.2f 就是保留小数点右边两位(四舍五入) >>> str1 '浮点数:199.16' >>> str1 = '浮点数:%.3f' % 199.1568 # %.3f 就是保留小数点右边三位(四舍五入) >>> str1 '浮点数:199.157'

3.字符串截取打印

1 2 3 4 5 6

>>> a = "从左往右打印3个字符:%.3s"%"abcdefg" # %.3s >>> a '从左往右打印3个字符:abc' >>> a = "从左往右打印5个字符:%.5s"%"abcdefg" # %.5s >>> a '从左往右打印5个字符:abcde'

4.打印百分号

1 2 3

>>> str1 = '浮点数:%.3f%%' % 199.1568 # %%打印出来就是一个% >>> str1 '浮点数:199.157%'

Python 的字符串内建函数

序号

方法

描述

1

capitalize()

将字符串的第一个字符转换为大写

2

center(width, fillchar)

返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。

3

count(str, beg= 0,end=len(string))

返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数

4

bytes.decode(encoding="utf-8", errors="strict")

Python3 中没有 decode 方法,但我们可以使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象,这个 bytes 对象可以由 str.encode() 来编码返回。

5

encode(encoding='UTF-8',errors='strict')

以 encoding 指定的编码格式编码字符串,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'

6

endswith(suffix, beg=0, end=len(string))

检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.

7

expandtabs(tabsize=8)

把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 。

8

find(str, beg=0 end=len(string))

检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1

9

index(str, beg=0, end=len(string))

跟find()方法一样,只不过如果str不在字符串中会报一个异常.

10

isalnum()

如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False

11

isalpha()

如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False

12

isdigit()

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

13

islower()

如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False

14

isnumeric()

如果字符串中只包含数字字符,则返回 True,否则返回 False

15

isspace()

如果字符串中只包含空白,则返回 True,否则返回 False.

16

istitle()

如果字符串是标题化的(见 title())则返回 True,否则返回 False

17

isupper()

如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False

18

join(seq)

以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串

19

len(string)

返回字符串长度

20

ljust(width[, fillchar])

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

21

lower()

转换字符串中所有大写字符为小写.

22

lstrip()

截掉字符串左边的空格或指定字符。

23

maketrans()

创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。

24

max(str)

返回字符串 str 中最大的字母。

25

min(str)

返回字符串 str 中最小的字母。

26

replace(old, new [, max])

把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次。

27

rfind(str, beg=0,end=len(string))

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

28

rindex( str, beg=0, end=len(string))

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

29

rjust(width,[, fillchar])

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

30

rstrip()

删除字符串字符串末尾的空格.

31

split(str="", num=string.count(str))

num=string.count(str)) 以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num 个子字符串

32

splitlines([keepends])

按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

33

startswith(str, beg=0,end=len(string))

检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。

34

strip([chars])

在字符串上执行 lstrip()和 rstrip()

35

swapcase()

将字符串中大写转换为小写,小写转换为大写

36

title()

返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())

37

translate(table, deletechars="")

根据 str 给出的表(包含 256 个字符)转换 string 的字符, 要过滤掉的字符放到 deletechars 参数中

38

upper()

转换字符串中的小写字母为大写

39

zfill (width)

返回长度为 width 的字符串,原字符串右对齐,前面填充0

40

isdecimal()

检查字符串是否只包含十进制字符,如果是返回 true,否则返回 false。

字符串常用操作

1,去掉空格和特殊符号

name.strip() 去掉空格和换行符

name.strip('xx') 去掉某个字符串

name.lstrip() 去掉左边的空格和换行符

name.rstrip() 去掉右边的空格和换行符

name.replace(' ','')删除或者替换空字符

2,字符串的搜索和替换

name.count('x') 查找某个字符在字符串里面出现的次数

name.capitalize() 首字母大写

name.center(n,'-') 把字符串放中间,两边用- 补齐

name.find('x') 找到这个字符返回下标,多个时返回第一个;不存在的字符返回-1

name.index('x') 找到这个字符返回下标,多个时返回第一个;不存在的字符报错

name.replace(oldstr, newstr) 字符串替换

name.format() 字符串格式化

name.format_map(d) 字符串格式化,传进去的是一个字典

Year {} Rs. {:.2f}".format(year, value) 称为字符串格式化,大括号和其中的字符会被替换成传入 str.format() 的参数,也即 year 和 value。其中 {:.2f} 的意思是替换为 2 位精度的浮点数。

{:5d} 的意思是替换为 5 个字符宽度的整数,宽度不足则使用空格填充。

divmod(num1,num2)返回一个元组,这个元组包含两个值,第一个是num1 与 num2 相整除得到的值,第二个是num1 与 num2 求余得到的值

用 * 运算符查封这两个元组,得到这两个值。

等价于

3,字符串的测试和替换函数

S.startswith(prefix[,start[,end]]) #是否以prefix开头 S.endswith(suffix[,start[,end]]) #以suffix结尾 S.isalnum() #是否全是字母和数字,并至少有一个字符 S.isalpha() #是否全是字母,并至少有一个字符 S.isdigit() #是否全是数字,并至少有一个字符 S.isspace() #是否全是空白字符,并至少有一个字符 S.islower() #S中的字母是否全是小写 S.isupper() #S中的字母是否便是大写 S.istitle() #S是否是首字母大写的

4,字符串的分割

name.split() 默认是按照空格分割

name.split(',') 按照逗号分割

5,连接字符串

‘,’.join(slit) 用逗号连接slit 变成一个字符串,slit 可以是字符,列表,字典(可迭代的对象)

int 类型不能被连接

6,截取字符串(切片)

切片操作(slice)可以从一个字符串中获取子字符串(字符串的一部分)。我们使用一对方括号、起始偏移量start、终止偏移量end 以及可选的步长step 来定义一个分片。

格式: [start:end:step]

• [:] 提取从开头(默认位置0)到结尾(默认位置-1)的整个字符串 • [start:] 从start 提取到结尾 • [:end] 从开头提取到end - 1 • [start:end] 从start 提取到end - 1 • [start:end:step] 从start 提取到end - 1,每step 个字符提取一个 • 左侧第一个字符的位置/偏移量为0,右侧最后一个字符的位置/偏移量为-1

str = '0123456789′ print str[0:3] #截取第一位到第三位的字符 print str[:] #截取字符串的全部字符 print str[6:] #截取第七个字符到结尾 print str[:-3] #截取从头开始到倒数第三个字符之前 print str[2] #截取第三个字符 print str[-1] #截取倒数第一个字符 print str[::-1] #创造一个与原字符串顺序相反的字符串 print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符 print str[-3:] #截取倒数第三位到结尾 print str[:-5:-3] #逆序截取

7.string 模块

代码语言:javascript
复制
import string 

string.ascii_uppercase  所有大写字母
string.ascii_lowercase 所有小写字母
string.ascii_letters  所有字母
string.digits  所有数字

分割字符串方法进行总结

第一种:split()函数 split()函数应该说是分割字符串使用最多的函数 用法: str.split('分割符') 通过该分割操作后,会返回一个列表。

注:当然如果你的字符串含有一个或者多个空格就直接 str.split() 就可以了

例如: >>> a = "hello,python,Good Night" >>> a.split(',') ['hello', 'python', 'Good Night']

第二种:splitlines()函数

splitline()函数是按“行”进行字符串分割

用法:

object.splitlines()

通过该分割操作后,会返回一个列表。

例如: >>> a = '''I have a pen I have a apple apple pen ''' >>> a.splitlines() ['I have a pen','I have a apple','apple pen']

注:a.如果上述的对象a中除了换行符,字符串前后还有空格的话,可以用strip()函数去除字符串前后的空格

b.对于splitlines()函数有一个keepends的bool型参数,当keepends为True时:分割的每 一行里尾部会有\n;当keepends为False时:不保留每行结尾的\n;

第三种:import re模块 进行字符串多种字符的分割

当我们在处理某些字符串的时候,需要在一个字符串中进行多个字符的分割,但是对于第一种方法split()一次只可以使用一个符号进行字符串分割操作,那么我们就可以使用这种方法

用法:

先导入re模块:import re

之后:re.split('分割符1|分割符2',objects) --> 不同的分割符用 ‘|’ 进行间隔(分割符需要进行'\'的转义操作),然后objects为需要分割的字符串对象。

例如: >>> e = "1111111@qq.com" >>> import re >>> re.split('@|\.',e) ['11111111', 'qq', 'com']

注意上面的 '.' 这个分割符进行了 '\.' 的转义表示 '.' 进行分割。

字符串常用操作函数

代码语言:javascript
复制
class str(basestring):
    """
    str(object='') -> string
    
    Return a nice string representation of the object.
    If the argument is a string, the return value is the same object.
    """
    def capitalize(self):  
        """ 首字母变大写 """
        """
        S.capitalize() -> string
        
        Return a copy of the string S with only its first character
        capitalized.
        """
        return ""

    def center(self, width, fillchar=None):  
        """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """
        """
        S.center(width[, fillchar]) -> string
        
        Return S centered in a string of length width. Padding is
        done using the specified fill character (default is a space)
        """
        return ""

    def count(self, sub, start=None, end=None):  
        """ 子序列个数 """
        """
        S.count(sub[, start[, end]]) -> int
        
        Return the number of non-overlapping occurrences of substring sub in
        string S[start:end].  Optional arguments start and end are interpreted
        as in slice notation.
        """
        return 0

    def decode(self, encoding=None, errors=None):  
        """ 解码 """
        """
        S.decode([encoding[,errors]]) -> object
        
        Decodes S using the codec registered for encoding. encoding defaults
        to the default encoding. errors may be given to set a different error
        handling scheme. Default is 'strict' meaning that encoding errors raise
        a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
        as well as any other name registered with codecs.register_error that is
        able to handle UnicodeDecodeErrors.
        """
        return object()

    def encode(self, encoding=None, errors=None):  
        """ 编码,针对unicode """
        """
        S.encode([encoding[,errors]]) -> object
        
        Encodes S using the codec registered for encoding. encoding defaults
        to the default encoding. errors may be given to set a different error
        handling scheme. Default is 'strict' meaning that encoding errors raise
        a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
        'xmlcharrefreplace' as well as any other name registered with
        codecs.register_error that is able to handle UnicodeEncodeErrors.
        """
        return object()

    def endswith(self, suffix, start=None, end=None):  
        """ 是否以 xxx 结束 """
        """
        S.endswith(suffix[, start[, end]]) -> bool
        
        Return True if S ends with the specified suffix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        suffix can also be a tuple of strings to try.
        """
        return False

    def expandtabs(self, tabsize=None):  
        """ 将tab转换成空格,默认一个tab转换成8个空格 """
        """
        S.expandtabs([tabsize]) -> string
        
        Return a copy of S where all tab characters are expanded using spaces.
        If tabsize is not given, a tab size of 8 characters is assumed.
        """
        return ""

    def find(self, sub, start=None, end=None):  
        """ 寻找子序列位置,如果没找到,返回 -1 """
        """
        S.find(sub [,start [,end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0

    def format(*args, **kwargs): # known special case of str.format
        """ 字符串格式化,动态参数,将函数式编程时细说 """
        """
        S.format(*args, **kwargs) -> string
        
        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').
        """
        pass

    def index(self, sub, start=None, end=None):  
        """ 子序列位置,如果没找到,报错 """
        S.index(sub [,start [,end]]) -> int
        
        Like S.find() but raise ValueError when the substring is not found.
        """
        return 0

    def isalnum(self):  
        """ 是否是字母和数字 """
        """
        S.isalnum() -> bool
        
        Return True if all characters in S are alphanumeric
        and there is at least one character in S, False otherwise.
        """
        return False

    def isalpha(self):  
        """ 是否是字母 """
        """
        S.isalpha() -> bool
        
        Return True if all characters in S are alphabetic
        and there is at least one character in S, False otherwise.
        """
        return False

    def isdigit(self):  
        """ 是否是数字 """
        """
        S.isdigit() -> bool
        
        Return True if all characters in S are digits
        and there is at least one character in S, False otherwise.
        """
        return False

    def islower(self):  
        """ 是否小写 """
        """
        S.islower() -> bool
        
        Return True if all cased characters in S are lowercase and there is
        at least one cased character in S, False otherwise.
        """
        return False

    def isspace(self):  
        """
        S.isspace() -> bool
        
        Return True if all characters in S are whitespace
        and there is at least one character in S, False otherwise.
        """
        return False

    def istitle(self):  
        """
        S.istitle() -> bool
        
        Return True if S is a titlecased string and there is at least one
        character in S, i.e. uppercase characters may only follow uncased
        characters and lowercase characters only cased ones. Return False
        otherwise.
        """
        return False

    def isupper(self):  
        """
        S.isupper() -> bool
        
        Return True if all cased characters in S are uppercase and there is
        at least one cased character in S, False otherwise.
        """
        return False

    def join(self, iterable):  
        """ 连接 """
        """
        S.join(iterable) -> string
        
        Return a string which is the concatenation of the strings in the
        iterable.  The separator between elements is S.
        """
        return ""

    def ljust(self, width, fillchar=None):  
        """ 内容左对齐,右侧填充 """
        """
        S.ljust(width[, fillchar]) -> string
        
        Return S left-justified in a string of length width. Padding is
        done using the specified fill character (default is a space).
        """
        return ""

    def lower(self):  
        """ 变小写 """
        """
        S.lower() -> string
        
        Return a copy of the string S converted to lowercase.
        """
        return ""

    def lstrip(self, chars=None):  
        """ 移除左侧空白 """
        """
        S.lstrip([chars]) -> string or unicode
        
        Return a copy of the string S with leading whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        If chars is unicode, S will be converted to unicode before stripping
        """
        return ""

    def partition(self, sep):  
        """ 分割,前,中,后三部分 """
        """
        S.partition(sep) -> (head, sep, tail)
        
        Search for the separator sep in S, and return the part before it,
        the separator itself, and the part after it.  If the separator is not
        found, return S and two empty strings.
        """
        pass

    def replace(self, old, new, count=None):  
        """ 替换 """
        """
        S.replace(old, new[, count]) -> string
        
        Return a copy of string S with all occurrences of substring
        old replaced by new.  If the optional argument count is
        given, only the first count occurrences are replaced.
        """
        return ""

    def rfind(self, sub, start=None, end=None):  
        """
        S.rfind(sub [,start [,end]]) -> int
        
        Return the highest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0

    def rindex(self, sub, start=None, end=None):  
        """
        S.rindex(sub [,start [,end]]) -> int
        
        Like S.rfind() but raise ValueError when the substring is not found.
        """
        return 0

    def rjust(self, width, fillchar=None):  
        """
        S.rjust(width[, fillchar]) -> string
        
        Return S right-justified in a string of length width. Padding is
        done using the specified fill character (default is a space)
        """
        return ""

    def rpartition(self, sep):  
        """
        S.rpartition(sep) -> (head, sep, tail)
        
        Search for the separator sep in S, starting at the end of S, and return
        the part before it, the separator itself, and the part after it.  If the
        separator is not found, return two empty strings and S.
        """
        pass

    def rsplit(self, sep=None, maxsplit=None):  
        """
        S.rsplit([sep [,maxsplit]]) -> list of strings
        
        Return a list of the words in the string S, using sep as the
        delimiter string, starting at the end of the string and working
        to the front.  If maxsplit is given, at most maxsplit splits are
        done. If sep is not specified or is None, any whitespace string
        is a separator.
        """
        return []

    def rstrip(self, chars=None):  
        """
        S.rstrip([chars]) -> string or unicode
        
        Return a copy of the string S with trailing whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        If chars is unicode, S will be converted to unicode before stripping
        """
        return ""

    def split(self, sep=None, maxsplit=None):  
        """ 分割, maxsplit最多分割几次 """
        """
        S.split([sep [,maxsplit]]) -> list of strings
        
        Return a list of the words in the string S, using sep as the
        delimiter string.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified or is None, any
        whitespace string is a separator and empty strings are removed
        from the result.
        """
        return []

    def splitlines(self, keepends=False):  
        """ 根据换行分割 """
        """
        S.splitlines(keepends=False) -> list of strings
        
        Return a list of the lines in S, breaking at line boundaries.
        Line breaks are not included in the resulting list unless keepends
        is given and true.
        """
        return []

    def startswith(self, prefix, start=None, end=None):  
        """ 是否起始 """
        """
        S.startswith(prefix[, start[, end]]) -> bool
        
        Return True if S starts with the specified prefix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        prefix can also be a tuple of strings to try.
        """
        return False

    def strip(self, chars=None):  
        """ 移除两段空白 """
        """
        S.strip([chars]) -> string or unicode
        
        Return a copy of the string S with leading and trailing
        whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        If chars is unicode, S will be converted to unicode before stripping
        """
        return ""

    def swapcase(self):  
        """ 大写变小写,小写变大写 """
        """
        S.swapcase() -> string
        
        Return a copy of the string S with uppercase characters
        converted to lowercase and vice versa.
        """
        return ""

    def title(self):  
        """
        S.title() -> string
        
        Return a titlecased version of S, i.e. words start with uppercase
        characters, all remaining cased characters have lowercase.
        """
        return ""

    def translate(self, table, deletechars=None):  
        """
        转换,需要先做一个对应表,最后一个表示删除字符集合
        intab = "aeiou"
        outtab = "12345"
        trantab = maketrans(intab, outtab)
        str = "this is string example....wow!!!"
        print str.translate(trantab, 'xm')
        """

        """
        S.translate(table [,deletechars]) -> string
        
        Return a copy of the string S, where all characters occurring
        in the optional argument deletechars are removed, and the
        remaining characters have been mapped through the given
        translation table, which must be a string of length 256 or None.
        If the table argument is None, no translation is applied and
        the operation simply removes the characters in deletechars.
        """
        return ""

    def upper(self):  
        """
        S.upper() -> string
        
        Return a copy of the string S converted to uppercase.
        """
        return ""

    def zfill(self, width):  
        """方法返回指定长度的字符串,原字符串右对齐,前面填充0。"""
        """
        S.zfill(width) -> string
        
        Pad a numeric string S with zeros on the left, to fill a field
        of the specified width.  The string S is never truncated.
        """
        return ""

    def _formatter_field_name_split(self, *args, **kwargs): # real signature unknown
        pass

    def _formatter_parser(self, *args, **kwargs): # real signature unknown
        pass

    def __add__(self, y):  
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y):  
        """ x.__contains__(y) <==> y in x """
        pass

    def __eq__(self, y):  
        """ x.__eq__(y) <==> x==y """
        pass

    def __format__(self, format_spec):  
        """
        S.__format__(format_spec) -> string
        
        Return a formatted version of S as described by format_spec.
        """
        return ""

    def __getattribute__(self, name):  
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y):  
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __getslice__(self, i, j):  
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y):  
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y):  
        """ x.__gt__(y) <==> x>y """
        pass

    def __hash__(self):  
        """ x.__hash__() <==> hash(x) """
        pass

    def __init__(self, string=''): # known special case of str.__init__
        """
        str(object='') -> string
        
        Return a nice string representation of the object.
        If the argument is a string, the return value is the same object.
        # (copied from class doc)
        """
        pass

    def __len__(self):  
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y):  
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y):  
        """ x.__lt__(y) <==> x<y """
        pass

    def __mod__(self, y):  
        """ x.__mod__(y) <==> x%y """
        pass

    def __mul__(self, n):  
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more):  
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y):  
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self):  
        """ x.__repr__() <==> repr(x) """
        pass

    def __rmod__(self, y):  
        """ x.__rmod__(y) <==> y%x """
        pass

    def __rmul__(self, n):  
        """ x.__rmul__(n) <==> n*x """
        pass

    def __sizeof__(self):  
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __str__(self):  
        """ x.__str__() <==> str(x) """
        pass
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年10月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python转义字符
  • Python字符串运算符
  • Python字符串格式化
  • Python 的字符串内建函数
    • 字符串常用操作
    • 分割字符串方法进行总结
    • 字符串常用操作函数
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档