*字符串不能更改值

数据类型字符串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. Debugging Under Unix: gdb Tutorial (https://www.cs.cmu.edu/~gilpin/tutorial/)

    //注释掉 #include <iostream.h> //替换为 #include <iostream> using namespace std; Contents Intr ...

  2. Memcached在企业中的应用

    Memcached简介 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitz ...

  3. [SketchUp]-绘制自己的家

    [SketchUp]-绘制自己的家 softsketchuphome 简介 最近已经完成了 毕业论文, 等待盲审的过程中, 将过去几年做的东西也都一一整理了, 硬盘中好几个不敢动的文件夹 也都可以删除 ...

  4. mysql硬件优化

    导致宕机的原因: 1.      运行环境: 35%,运行环境可以看做是支持数据库服务器运行的系统和资源集合,包括操作系统,硬盘以及网络 2.      性能:35% 3.      复制 20% 4 ...

  5. 基于ONOS的T-SDN Super控制器,突破多域网络运营的难关

    在法国尼斯举办的IIR WDM波分论坛(Next Generation Optical Networking)上,华为展示了T-SDN(Transport SDN,传送SDN)领域的众多新技术,其中T ...

  6. #if 和#ifdef的区别

    转自:https://blog.csdn.net/zhangchiytu/article/details/7563329 先看个例子:#define TARGET_LITTLE_ENDINA 1#de ...

  7. Eclipse 全部快捷一览表(具TM全)

    1. 编辑快捷键 编辑快捷键 介绍 psvm + Tab 生成main方法 sout + tab 生成输出语句 Ctrl+X / Ctrl + Y 删除一行 Ctrl+D 复制一行 Ctrl+/ 或 ...

  8. 图论--网络流--最大流 POJ 2289 Jamie's Contact Groups (二分+限流建图)

    Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very ...

  9. Eating Everything Efficiently(反向dp)

    传送门 取最大值即可.用拓扑,dfs都可以实现 #include <bits/stdc++.h> using namespace std; const int maxn=500009; i ...

  10. C. Jury Marks 思维

    C. Jury Marks 这个题目虽然是只有1600,但是还是挺思维的. 有点难想. 应该可以比较快的推出的是这个肯定和前缀和有关, x x+a1 x+a1+a2 x+a1+a2+a3... x+s ...