*字符串不能更改值

数据类型字符串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. HTML JavaScript 基础(上)

    一.初识JavaScript JavaScript 和 Java什么关系? 半毛线关系都没有,只是名字有点重合而已. JavaScript 和python.C#.Java.Ruby一样,都是一门独立的 ...

  2. Java Web:jstl处理字符串

    用法:${fn:methodName(args....)} 在使用这些函数之前必须在JSP中引入标准函数的声明<%@ taglib prefix="fn" uri=" ...

  3. 崛起于Springboot2.X之开发拦截器(21)

    为什么80%的码农都做不了架构师?>>>   序言:几乎所有项目都需要拦截器,所以小伙伴们必须要掌握这门技术哦,不然只会mybaits增删改查那是实习生干的活呀. 1.创建拦截器类, ...

  4. 图论--拓扑排序--HDU-1285确定比赛名次

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  5. ACM成长之路(干货) 我爱ACM,与君共勉

    前几天在网上看到,转过来时刻督促一下自己. ACM队不是为了一场比赛而存在的,为的是队员的整体提高. 大学期间,ACM队队员必须要学好的课程有: l C/C++两种语言 l 高等数学 l 线性代数 l ...

  6. FTP服务器项目的一些整理

    几个月前按照网上的教程写了一个FTP的服务器,现在回头整理一下里面的一些知识. FTP简介 FTP是文件传输协议(File Transfer Protocol),工作在TCP/IP协议族的应用层,其传 ...

  7. vue element select多选回显

    我们经常在使用 Element组件里面的 select多选 场景:添加账号的时候需要选择可见分公司(分公司为多选),添加成功之后可以编辑,需要回显添加时所提交的分公司 代码如下: 多选框: data( ...

  8. libevent(四)event_base 2

    接上文libevent(三)event_base event_io_map event_list是双向链表,min_heap是小根堆,那event_io_map是什么呢? #ifdef WIN32 # ...

  9. 翻转单词顺序 VS 左旋转字符串

    全部内容来自<剑指offer>. 题目一: 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字符一样处理.例如输入字符串“I am a stude ...

  10. Android | 带你零代码实现安卓扫码功能

    目录 小序 背景介绍 前期准备 开始搬运 结语 小序   这是一篇纯新手教学,本人之前没有任何安卓开发经验(尴尬),本文也不涉及任何代码就可以使用一个扫码demo,华为scankit真是新手的福音-- ...