*字符串不能更改值

数据类型字符串str

  • |  capitalize(...)   返回字符串中第一个字母大写
     |      S.capitalize() -> str
     |      
     |      Return a capitalized version of S, i.e. make the first character
     |      have upper case and the rest lower case.

    >>> str1='i am chinese'
    >>> str1.capitalize()
    'I am chinese'
  • |  casefold(...)  将大写转换成小写
     |      S.casefold() -> str
     |      
     |      Return a version of S suitable for caseless comparisons.
    >>> str2='I AM A CHINESE'
    >>> str2.casefold()
    'i am a chinese'
  • |  center(...)  第一个参数是总字符长度,第二个是补全字符,原字符串居中
     |      S.center(width[, fillchar]) -> str
     |      
     |      Return S centered in a string of length width. Padding is
     |      done using the specified fill character (default is a space)
    >>> str1.center(50,'#')
    '###################i am chinese###################'
  • |  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.
    >>> str1.count('e')
    2
    >>> str1.count('i')
    2
    >>> str1.count('a')
    1
  • |  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.
    >>> str2.endswith('SE')
    True
    >>> str2.endswith('S')
    False
    >>> str = "this is string example....wow!!!";
    >>> suffix = "is"
    >>> print (str.endswith(suffix, 2, 6))
    False
    >>> print(str[2:7])
    is is
    #字符串含有指定后缀,为什么不是返回真?
    #理解错误,起始位置是左闭右开
    >>> print (str.endswith(suffix, 2,7))
    True
  • |  expandtabs(...) 扩展tab字符n个
     |      S.expandtabs(tabsize=8) -> str
     |      
     |      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.
    >>> str4='I am \t Chinese'
    >>> str4.expandtabs(10)
    'I am Chinese'
  • |  find(...)  返回查找到的第一个字符位置,否则返回-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.
    >>> str2
    'I AM A CHINESE'
    >>> str2.find('M')
    3
    >>> str2.find('A')
    2
    >>> str2.find('B')
    -1
  • |  index(...)  同上,查找不到则报错
     |      S.index(sub[, start[, end]]) -> int
     |      
     |      Like S.find() but raise ValueError when the substring is not found.
    >>> str2
    'I AM A CHINESE'
    >>> str2.index('A')
    2
    >>> str2.index('B')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: substring not found
  • |  isalnum(...)  判断字符串是否由字母数字组成
     |      S.isalnum() -> bool
     |      
     |      Return True if all characters in S are alphanumeric
     |      and there is at least one character in S, False otherwise.
    >>> str5='I am 1 chinese'
    >>> str5.isalnum()
    False
    >>> str6='iAm1Chinese'
    >>> str6.isalnum()
    True
  • |  isalpha(...)  判断字符串是否全部是字符组成
     |      S.isalpha() -> bool
     |      
     |      Return True if all characters in S are alphabetic
     |      and there is at least one character in S, False otherwise.
    >>> str7='iamChinese'
    >>> str7.isalpha()
    True
    >>> str3.isalpha()
    False
  • |  isdecimal(...) 如果字符串是否只包含十进制字符返回True,否则返回False
     |      S.isdecimal() -> bool
     |      
     |      Return True if there are only decimal characters in S,
     |      False otherwise.
    >>> str8=u'asdfasdf1000'
    >>> str8.isdecimal()
    False
    >>> stru=u'1000'
    >>> stru.isdecimal()
    True
  • |  isdigit(...)  判断字符串中是否全部是数字
     |      S.isdigit() -> bool
     |      
     |      Return True if all characters in S are digits
     |      and there is at least one character in S, False otherwise.
    >>> str9,str10='abc123','123'
    >>> str9.isdigit()
    False
    >>> str10.isdigit()
    True
  • |  isidentifier(...) 判断变量明明是否合法
     |      S.isidentifier() -> bool
     |      
     |      Return True if S is a valid identifier according
     |      to the language definition.
     |      
     |      Use keyword.iskeyword() to test for reserved identifiers
     |      such as "def" and "class".
  • |  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.
    >>> str11,str12,str13='abcd','AbCd','Ab123'
    >>> str11.islower()
    True
    >>> str12.islower()
    False
    >>> str13.islower()
    False
    >>> str14='aa123'
    >>> str14.islower()
    True
  • |  isnumeric(...) 判断字符串中所有字符是否是数字
     |      S.isnumeric() -> bool
     |      
     |      Return True if there are only numeric characters in S,
     |      False otherwise.
    >>> str10,str14
    ('123', 'aa123')
    >>> str10.isnumeric()
    True
    >>> str14.isnumeric()
    False
  • |  isspace(...) 判断字符是否是空格
     |      S.isspace() -> bool
     |      
     |      Return True if all characters in S are whitespace
     |      and there is at least one character in S, False otherwise.
    >>> str1,str14,str15
    ('i am chinese', 'aa123', ' ')
    >>> str1.isspace(),str14.isspace(),str15.isspace()
    (False, False, True)
  • |  istitle(...) 判断字符串是否是标题,标题单词首字母均大写
     |      S.istitle() -> bool
     |      
     |      Return True if S is a titlecased string and there is at least one
     |      character in S, i.e. upper- and titlecase characters may only
     |      follow uncased characters and lowercase characters only cased ones.
     |      Return False otherwise.
    >>> str16,str17='I Am Chinese','I am chinese'
    >>> str16.istitle(),str17.istitle()
    (True, False)
  • |  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.
    >>> str18,str19='ABC123','Abc123'
    >>> str18.isupper(),str19.isupper()
    (True, False)
  • |  join(...) 将其他可迭代对象转换成字符串,S表示字符串分隔符
     |      S.join(iterable) -> str
     |      
     |      Return a string which is the concatenation of the strings in the
     |      iterable.  The separator between elements is S.
    >>> list1=['a','b','c','d']
    >>> ','.join(list1)
    'a,b,c,d'
    >>> ''.join(list1)
    'abcd'
    >>> ' '.join(list1)
    'a b c d'
  • |  ljust(...)  共width个位置,字符串靠左,右边以字符填充
     |      S.ljust(width[, fillchar]) -> str
     |      
     |      Return S left-justified in a Unicode string of length width. Padding is
     |      done using the specified fill character (default is a space).
    >>> str10
    '123'
    >>> str10.ljust(10,'#')
    '123#######'
  • |  lower(...)  将字符串中所有大写字母转换成小写
     |      S.lower() -> str
     |      
     |      Return a copy of the string S converted to lowercase.
  • |  lstrip(...)  将左边空格剪掉,可以指定其他被剪掉空格,\n \t
     |      S.lstrip([chars]) -> str
     |      
     |      Return a copy of the string S with leading whitespace removed.
     |      If chars is given and not None, remove characters in chars instead.
    >>> str20='  123abc  ABC  '
    >>> str20.lstrip()
    '123abc ABC '

    strip()将左右两边空格剪掉,rstrip()将右边空格剪掉

  • |  partition(...) 以某个字符切片字符串,返回三元组,中间是分隔符,找不到则返回源字符串和2个空格
     |      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.
    >>> str21='http://www.baidu.com'
    >>> str21.partition('://'... )
    ('http', '://', 'www.baidu.com')
    >>> str21.partition('abc')
    ('http://www.baidu.com', '', '')
  • |  replace(...)  字符替换,默认全部替换,如指定计数则替换指定计数个数字符
     |      S.replace(old, new[, count]) -> str
     |      
     |      Return a copy of S with all occurrences of substring
     |      old replaced by new.  If the optional argument count is
     |      given, only the first count occurrences are replaced.
    >>> str21
    'http://www.baidu.com'
    >>> str21.replace('baidu','jd')
    'http://www.jd.com'
    >>> str21.replace('w','z')
    'http://zzz.baidu.com'
    >>> str21.replace('w','z',1)
    'http://zww.baidu.com'
  • |  rfind(...) 从左往右查找,并返回最右边的字符索引位置
     |      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.
    >>> str21
    'http://www.baidu.com'
    >>> str21.rfind('b')
    11
    >>> str21.rfind('w')
    9
  • |  rindex(...)
     |      S.rindex(sub[, start[, end]]) -> int
     |      
     |      Like S.rfind() but raise ValueError when the substring is not found.
  • |  rjust(...) 字符靠右,左边补全字符,默认空格
     |      S.rjust(width[, fillchar]) -> str
     |      
     |      Return S right-justified in a string of length width. Padding is
     |      done using the specified fill character (default is a space).
     |  rjust(...)
    | S.rjust(width[, fillchar]) -> str
    |
    | Return S right-justified in a string of length width. Padding is
    | done using the specified fill character (default is a space).
    >>> str10.rjust(10,'x')
    'xxxxxxx123'
  • |  rpartition(...) 无法找到切片字符串,则源字符串索引是2,前面是空格
     |      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.
    >>> str21.rpartition('://')
    ('http', '://', 'www.baidu.com')
    >>> str21.rpartition('abc')
    ('', '', 'http://www.baidu.com')
  • |  split(...) 默认分隔符空格 \t \n,可以指定分割次数
     |      S.split(sep=None, maxsplit=-1) -> list of strings
     |      
     |      Return a list of the words in 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.
    >>> str21.split('w')
    ['http://', '', '', '.baidu.com']
    >>> str21.split('w',1)
    ['http://', 'ww.baidu.com']
    >>> str21.split('w',2)
    ['http://', '', 'w.baidu.com']
    >>> str21.split('w',3)
    ['http://', '', '', '.baidu.com']
  • |  rsplit(...) 右边第一个分割符开始切分,默认全切分,可以指定分割次数
     |      S.rsplit(sep=None, maxsplit=-1) -> list of strings
     |      
     |      Return a list of the words in 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, any whitespace string
     |      is a separator.
    >>> str21.rsplit('w')
    ['http://', '', '', '.baidu.com']
    >>> str21.rsplit('w',1)
    ['http://ww', '.baidu.com']
    >>> str21.rsplit('w',2)
    ['http://w', '', '.baidu.com']
    >>> str21.rsplit('w',3)
    ['http://', '', '', '.baidu.com']
  • |  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.
    >>> str22='www\nbaidu\ncom'
    >>> str22.splitlines()
    ['www', 'baidu', 'com']
  • |  startswith(...)  同endswith()
     |      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.
  • |  swapcase(...)   字符串中大小写转换,大写变小写,小写变大写--- upper(),lower()
     |      S.swapcase() -> str
     |      
     |      Return a copy of S with uppercase characters converted to lowercase
     |      and vice versa.
    >>> str23='asdf123'
    >>> str23.swapcase()
    'ASDF123'
    >>> str24='ABcd222'
    >>> str24.swapcase()
    'abCD222'
  • |  title(...)  返回一个titile标题
     |      S.title() -> str
     |      
     |      Return a titlecased version of S, i.e. words start with title case
     |      characters, all remaining cased characters have lower case.
    >>> str25='a good persion asdf 213'
    >>> str25.title()
    'A Good Persion Asdf 213'
  • |  zfill(...)  总长width个字符,前面补0
     |      S.zfill(width) -> str
     |      
     |      Pad a numeric string S with zeros on the left, to fill a field
     |      of the specified width. The string S is never truncated.
    >>> str25.zfill(30)
    '0000000a good persion asdf 213'

python3.x 基础一:str字符串方法的更多相关文章

  1. Python基础7:字符串方法

    1 * 重复输出字符串 print('helo '*4) 2 [],[:] 通过索引获取字符串中的字符,这里和列表中的切片操作是相同的,具体内容见列表 print('hello word'[2:]) ...

  2. Python基础(数字,字符串方法)

    数字: #二进制转十进制 a=' v=int(a,base=2) print(v) 进制转换 #当前数字的二进制至少有多少位 b=2 v2=b.bit_length() print(v2) 数值二进制 ...

  3. Python基础数据类型str字符串

    3.3字符串str ' ' 0 切片选取 [x:y] 左闭右开区间 [x:y:z] 选取x到y之间 每隔z选取一次(选取x,x+z,....) z为正 索引位置:x在y的左边 z为负 索引位置:x在y ...

  4. python基础-生成随机字符串方法

    python解释器示例 >>> import uuid >>> uuid.uuid1() UUID('ae6822e6-c976-11e6-82e0-0090f5f ...

  5. python基础学习笔记——字符串方法

    索引和切片: 索引:取出数组s中第3个元素:x=s[2] 切片:用极少的代码将数组元素按需处理的一种方法.切片最少有1个参数,最多有3个参数,演示如下: 我们假设下面所用的数组声明为array=[2, ...

  6. Python字符串str的方法使用

    #!usr/bin/env python# -*-coding:utf-8-*-#字符串通常用双引号或单引号来表示:'123',"abc","字符串"#str字 ...

  7. C#基础之操作字符串的方法

    C#基础之操作字符串的方法 C#中封装的对字符串操作的方法很多,下面将常见的几种方法进行总结: 首先定义一个字符串str 1.str.ToCharArray(),将字符串转换成字符数组 2.str.S ...

  8. 字符串str.format()方法的个人整理

    引言: 字符串的内置方法大致有40来个,但是一些常用的其实就那么20几个,而且里面还有类似的用法,区分度高比如isalpha,isalnum,isdigit,还有一些无时不刻都会用到的split切分, ...

  9. Python基础 第三章 使用字符串(3)字符串方法&本章小结

    字符串的方法非常之多,重点学习一些最有用的,完整的字符串方法参见<Python基础教程(第三版)>附录B. 模块string,虽然风头已小,但其包含了一些字符串方法中没有的常量和函数,故将 ...

随机推荐

  1. Windows 版本 Enterprise、Ultimate、Home、Professional

    关于Windows 的安装光盘版本很多种,很多人不知道选择哪些. Ultimate 旗舰版,VISTA开始有了这个级别,是最全最高级的,一般程序开发的电脑,玩游戏的电脑,建议用它,不过对配置稍有一些要 ...

  2. [Inno Setup] 字符串列表,当要处理一长串文件时很有用

    https://wiki.freepascal.org/TStringList-TStrings_Tutorial TStringList-TStrings Tutorial │ Deutsch (d ...

  3. beetl 模板语法

    如何定义临时变量 @var tmp = false; 如何给临时变量赋值 @tmp = true; 如何在判断中使用临时变量 @if(tmp){ ... @} 如何使用条件语句 if else @if ...

  4. 在java中构建高效的结果缓存

    文章目录 使用HashMap 使用ConcurrentHashMap FutureTask 在java中构建高效的结果缓存 缓存是现代应用服务器中非常常用的组件.除了第三方缓存以外,我们通常也需要在j ...

  5. Scala的Higher-Kinded类型

    Scala的Higher-Kinded类型 Higher-Kinded从字面意思上看是更高级的分类,也就是更高一级的抽象.我们先看个例子. 如果我们要在scala中实现一个对Seq[Int]的sum方 ...

  6. Django入门2:路由系统

    1.单一路由对应 url(r'^index/', views.index), # FBV url(r'^home/', views.Home.as_view()), # CBV 2.基于正则的路由 u ...

  7. 【React踩坑记五】React项目中引入并使用react-ace代码编辑插件(自定义列表提示)

    最近有一个引入sql编辑器插件的需求,要求代码高亮显示,代码智能提示,以及支持自定义代码提示列表等功能.中途在自定义代码提示列表中由于没有相关demo,所以踩了一些坑,遂将其整理如下,以便日后查看. ...

  8. 开始导入第一个第三方库jieba

    在做python的练习题,想看看运行结果. 谁知,有道题,不能识别jieba,原来要导入,因为是第三方库,照着书里面的导入方法,有三种,一种是用pip,在命令行里面安装,使用pip - p 可以查看p ...

  9. INTERVIEW #0

    一.造成网络延迟的可能原因 1,WiFi所有用户上下行流量共用一个信道,当用户太多或者有人在下载大的资源时带宽不够,丢包: 2,线路质量不佳导致信噪比太低,比如光纤损耗太大等. 二.IPv6优势 1, ...

  10. C++编程入门题目--No.3

    题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析: 在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后 的结 ...