python str

capitalize(...)

|      S.capitalize() -> string

|      #-字符串首字母大写-

|      Return a copy of the string S with only its first character

|      capitalized.

|

|  center(...)

|      S.center(width[, fillchar]) -> string

|      #字符串居中,width表示修改后字符串总长度,fillchar表示多余的长度位置填充什么样的字符:比如‘&’,‘1’,等字符或符号。

|      Return S centered in a string of length width. Padding is

|      done using the specified fill character (default is a space)

|

|  count(...)

|      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.

decode(...)

|      S.decode([encoding[,errors]]) -> object

|      #解码:unicode  utf-8 gbk 等编码的中文识别。unicode 万国码,utf-8 Python 默认编码,GBK Windows编码
          python---->windows     unicode=decode(‘utf-8‘)        gbk=unicode(‘gbk‘)

|      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.

|

|  encode(...)

|      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.

endswith(...)

|      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.

|

|  expandtabs(...)

|      S.expandtabs([tabsize]) -> string

|      #将TAB空格转换成8个相应的空格

|      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.

|

|  find(...)

|      S.find(sub [,start [,end]]) -> int

|      #查找字符串中的字符或短句,返回找到的第一个字符或短句的位置信息,未找到返回-1

|      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.

format(...)

|      #格式化输入/输出   比如:print  ‘{0}************{1}’.format(‘s‘,‘b‘)

S.format(*args, **kwargs) -> string

|

|  index(...)

  #同find函数,不同之处未找到就报错。

|      S.index(sub [,start [,end]]) -> int

|

|      Like S.find() but raise ValueError when the substring is not found.

|

|  isalnum(...)

   #判断字符串是否只含字母和数字字符,是返回True否返回Flase

|      S.isalnum() -> bool

|

|      Return True if all characters in S are alphanumeric

|      and there is at least one character in S, False otherwise.

|

|  isalpha(...)

  #判断字符串是否只含字母

|      S.isalpha() -> bool

|

|      Return True if all characters in S are alphabetic

|      and there is at least one character in S, False otherwise.

|

|  isdigit(...)

   #判断字符串中是否只含数字字符

|      S.isdigit() -> bool

|

|      Return True if all characters in S are digits

|      and there is at least one character in S, False otherwise.

islower(...)

#判断字符串中的字母是否全部是小写
| 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.
|
|  isspace(...)

#判断字符串里是否全部是空格,全空格返回True
| S.isspace() -> bool
|
| Return True if all characters in S are whitespace
| and there is at least one character in S, False otherwise.
|
|  istitle(...)

#判断字符串中首字母是否是大写并且其余字母小写
| 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.

isupper(...)

#判断是否全部是大写
| 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.
|
| join(...)

#将多个字符或字符串组合成一个字符串。如 ‘ ’.join(s)
| S.join(iterable) -> string
|
| Return a string which is the concatenation of the strings in the
| iterable. The separator between elements is S.
|
| ljust(...)

#左对齐,右填充字符。同center
| 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).
|
| lower(...)

#全部转小写
| S.lower() -> string
|
| Return a copy of the string S converted to lowercase.

lstrip(...)

#strip()为去掉字符串前后的空格,lstrip与rstrip为去掉左端或者右端的空格
| 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

partition(...)

#它用来根据指定的分隔符将字符串进行分割,如果字符串包含指定的分隔符,则返回一个3元的tuple,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。第二个例子说明,如果找不到指定的分隔符,则返回仍然是一个3元的tuple,第一个为整个字符串,第二和第三个为空串。

它与split(sep, 1)有什么区别呢?首先split返回的可能不是固定长度的返回值,它返回的是一个list,如果找到,则返回一个2元list,如果没找到,则返回一个1元的list,如:

>>> ‘a.b.c’.split(‘,’, 1)
[‘a.b.c‘]
>>> ‘a.b.c’.split(‘.’, 1)
[‘a‘, ‘b.c‘]

| 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.
|
| replace(...)

#字符串替换。例如:s.replace(‘a‘,‘b‘)将字符串中的‘a‘全部替换成‘b’
| 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.
|
| rfind(...)

#从右开始查找,未找到返回-1
| 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.

rindex(...)

#从右开始。。。同index
| S.rindex(sub [,start [,end]]) -> int
|
| Like S.rfind() but raise ValueError when the substring is not found.
|
| rjust(...)

#从右开始
| 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)
|
| rpartition(...)

#从右开始
| 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.

rsplit(...)

#从右开始分割,默认以空格,或者可以在括号内写分割要求。例如split(‘o‘,2) 代表用O作为分割位,切2次。
| 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.

rstrip(...)

#移除字符串头尾指定的字符(默认为空格)
| 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
|
| split(...)

#将字符串分割成列表,默认以空格分割
| 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.

splitlines(...)

| S.splitlines([keepends]) -> 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.
|
| startswith(...)
| 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.
|
| strip(...)
| 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

时间: 2024-10-14 14:33:21

python str的相关文章

PyQt QString 与 Python str&unicode

昨日,将许久以前做的模拟网页登录脚本用PyQt封装了一下,结果出大问题了, 登录无数次都提示登录失败!!而不用PyQt实现的GUI登录直接脚本登录无数次都提示登录成功!!心中甚是伤痛,于是探究起来,解决这一问题. 问题描述及证据如下: 上图是脚本MD5加密过程及结果 上图是PyQt GUI中获取密码框内容后加密的结果,其实现代码如下: # -*- coding: gbk -*- ''' Version : Python27 Author : Spring God Date : 2013-6-28

Python -- str 类

Python str类常用方法: class str(object): def capitalize(self):   # 全部字母变小写只有首字母变大写: >>> test = 'PYTHON' >>> test.capitalize() 'Python' def casefold(self): # 全部字母变小写: >>> test = 'PYTHON' >>> test.casefold() 'python' def cente

Python str()函数

描述str函数是Python的内置函数,它将参数转换成字符串类型,即人适合阅读的形式. 语法str(object)名称 说明 备注object 待被转换成字符串的参数 可省略的参数返回值:返回object的字符串形式 使用示例1. 无参调用当str()函数的参数都省略时,函数返回空字符串.这种情况常用来创建空字符串或者初始化字符串变量. >>> str()''2. 不省略参数str函数将整数.浮点数.列表.元组.字典和集合转换为字符串类型 >>> str(-23) #整

Python str字符串常用到的函数

# -*- coding: utf-8 -*- x='pythonnnnnnoooo' print type(x) # <type 'str'> 输出类型 print x.capitalize() # Pythonnnnnnoooo 首字符大写 print x.center(40) # pythonnnnnnoooo 类似于对其 print x.count('o') # 5 统计字符出现的次数 print x.count('o',4,6) # 1 指定区间统计字符出现的次数 print x.e

python str的一些方法

在python有各种各样的string操作函数.在历史上string类在python中经历了一段轮回的历史.在最开始的时候,python有一个专门的string的module,要使用string的方法要先import,但后来由于众多的python使用者的建议,从python2.0开始,string方法改为用S.method()的形式调用,只要S是一个字符串对象就可以这样使用,而不用import.同时为了保持向后兼容,现在的python中仍然保留了一个string的module,其中定义的方法与S

Python str repr的区别

尽管str(),repr()和``运算在特性和功能方面都非常相似,事实上repr()和``做的是完全一样的事情,它们返回的是一个对象的“官方”字符串表示,也就是说绝大多数情况下可以通过求值运算(使用内建函数eval())重新得到该对象. 但str()则有所不同,str()致力于生成一个对象的可读性好的字符串表示,它的返回结果通常无法用于eval()求值,但很适合用于print语句输出.需要再次提醒的是,并不是所有repr()返回的字符串都能够用 eval()内建函数得到原来的对象. 也就是说 r

Python str方法总结

1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string >>>a = 'shaw' >>> b = a.capitalize() >>> print b Shaw 2.按指定长度填充特定字符 center(...) S.center(width[, fillchar]) -> string >>> a = 'linux' >>> print a.cen

python str.translate()函数用法

Python translate()方法 描述 Python translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中. 语法 translate()方法语法: str.translate(table[, deletechars]); 参数 table -- 翻译表,翻译表是通过maketrans方法转换而来. deletechars -- 字符串中要过滤的字符列表. 返回值 返回翻译后的字符串. 实例 以下实例展示了

python str 与repr区别

Python打印值的时候会保持该值在Python代码中的状态,不是用户所希望看到的状态.而使用print打印值则不一样,print打印出来的值是用户所希望看到的状态. 例如: >>> "Hello, world!" 'Hello, world!' # Python打印出来的值是给python理解的,这里python理解为字符串,所以带着引号 >>> 1000L # python理解为Long型的数字,所以它打印出来的时候也带着后缀L 1000L &g

Python str方法

startwith方法是用来测试字符串是否以给定字符串开始. in操作符用来检验一个给定字符串是否为另一个字符串的一部分. find方法用来找出给定字符串在另一个字符串中的位置,或者返回-1以表示找不到子字符串. str类也有以一个作为分隔符的字符串join序列的项目的整洁的方法,它返回一个生成的大字符串. #!/usr/bin/python # Filename: str_methods.py name = 'Swaroop' # This is a string object if name