python3 字符串属性

 >>> a='hello world'
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
1、S.capitalize() -> str   把字符串的第一个字符大写
 >>> a='hello world'
>>> a.capitalize()
'Hello world'
2、S.center(width[, fillchar]) -> str 返回一个原字符串居中,并使用空格填充至长度width 的新字符串
 >>> a.center()
' hello world '
3、S.count(sub[, start[, end]]) -> int 返回str 在S 里面出现的次数,

 >>> a='hello world'
>>> a.count('l',) >>> a.count('l',,)
4、S.casefold() 将字符串所有字符改为小写(涉及到其他语言)
     S.lower()将字符串所有字符改为小写
 >>> a='HELLO WORLD'
>>> a.casefold()
'hello world'
>>> a
'HELLO WORLD'
>>> a.lower()
'hello world'
>>> a
'HELLO WORLD'

5、字符串编解码

{
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode
作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编(encode)
成另一种编码。但是,Python 2.x的默认编码格式是ASCII,就是说,在没有指定Python源码编码
格式的情况下,源码中的所有字符都会被默认为ASCII码。也因为这个根本原因,在Python 2.x中
经常会遇到UnicodeDecodeError或者UnicodeEncodeError的异常。

原则

decode early, unicode everywhere, encode late,即:在输入或者声明字符串的时候,
尽早地使用decode方法将字符串转化成unicode编码格式;然后在程序内使用字符串的时候统一使用
unicode格式进行处理,比如字符串拼接、字符串替换、获取字符串的长度等操作;最后,在输出字符
串的时候(控制台/网页/文件),通过encode方法将字符串转化为你所想要的编码格式,比如utf-8等。
https://segmentfault.com/a/1190000002966978
}
S.encode(encoding='utf-8', errors='strict') -> bytes 以encoding指定的编码格式对字符串进行编码,输出的字节不是字符串,类型不同,属性不同。


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

  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.

 >>> a='hello world!'
>>> a.endswith('!')
True
>>> a.endswith('o',,)
True
>>> a.startswith('h')
True
>>> a.startswith('w',)
True
>>> a.startswith('hello')
True

7、 S.expandtabs(tabsize=8) -> str  把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格

 >>> a='hello world'
>>> a.expandtabs()
'hello world'
>>> a='\t hello world \t'
>>> a.expandtabs()
' hello world '
>>> a.expandtabs(tabsize=)
' hello world '

8、S.find(sub[, start[, end]]) -> int   检测sub是否在字符串中,如果在则返回index,否则返回-1,start,end为可选参数,决定范围。(返回左侧第一个)

 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.

 >>> a='hello world'
>>> a.find('h') >>> a.find('h',,)
-
>>> a.find('o',,)
-
>>> a.find('o',,)

9、

S.index(sub[, start[, end]]) -> int     没有找到返回ValuueError错误        (返回左侧第一个)

Like S.find() but raise ValueError when the substring is not found.   没有找到返回 -1

S.rindex(sub[, start[, end]]) -> int          (返回右侧第一个)
    
    Like S.rfind() but raise ValueError when the substring is not found.

 >>> a
'hello world'
>>> a.index('ll') >>> a.index('lll')
Traceback (most recent call last):
File "<stdin>", line , in <module>
ValueError: substring not found

10、S.isalnum() -> bool  Return True if all characters in S are alphanumeric

都是字母和数字字符返回True,否则返回False

>>> a.isalnum()
False
>>> a='123#$":,./'
>>> a.isalnum()
False
>>> a='123'
>>> a.isalnum()
True
>>> a='123a'
>>> a.isalnum()
True
>>> a='123a('
>>> a.isalnum()
False

11、S.isalpha() -> bool  Return True if all characters in S are alphabetic

判断字符串是否为字母

>>> a='123'
>>> b='123a'
>>> c='abc'
>>> a.isalpha()
False
>>> b.isalpha()
False
>>> c.isalpha()
True

  

python3 字符串属性(一)的更多相关文章

  1. python3 字符串属性(四)

    1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...

  2. python3 字符串属性(三)

    maketrans 和 translate的用法(配合使用) 下面是python的英文用法解释 maketrans(x, y=None, z=None, /) Return a translation ...

  3. python3字符串属性(二)

    1.S.isdecimal() -> bool    Return True if there are only decimal characters in S, False otherwise ...

  4. NSAttributedString字符串属性类

    //定义一个可变字符串属性对象aStr NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithStri ...

  5. 字符串属性使用strong的原因

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  6. mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符

    连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... li ...

  7. python3字符串

    Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...

  8. JavaScript 常用内置对象(字符串属性、Math对象、Array数组对象)

    1.字符串属性   <script>   var test_var = "I Iove you"; console.log(test_var.charAt(3)) // ...

  9. [转]python3字符串与文本处理

    转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...

随机推荐

  1. Ext如何Gird中如何显示时间类型的列?

         rt,后台bean中的字段属性是Date类型,前台的model中的字段属性也设置为date类型,但是还是显示不出来,如何解决呢? 直接代码: grid中的COLUMN: Model中的定义:

  2. shadow密码文件

    登录Linux会要求输入用户名和密码.通常本地文件中会存储一份用户密码,并与用户输入对比,如果相同就允许用户登录.起初用户密码存储与/etc/passwd中,但由于/etc/passwd必须供所有用户 ...

  3. 【Mac系统】之破解Navicat Premium for Mac(中文版)

    前期准备工作: 下载Mac中文版本: 下载地址:链接:https://pan.baidu.com/s/1wShOFK9odsSWBSphgqTdEQ  密码:ipsa 下载完成后进行安装,安装步骤省略 ...

  4. 【Mac + Python】苹果系统之安装Python3.6.x环境

    一.打开终端 输入:uname -a  ,查看电脑系统位数. 输入:python,查看mac系统python版本. 二.为了以后切换版本方便,安装pyenv进行版本切换以及升级. 参考文章:<M ...

  5. diamond源码阅读-目录监控

    PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...

  6. js校验密码强度

    网上转载的一段代码,留着以后用, js文件: //判断输入密码的类型 function CharMode(iN){ if (iN>=48 && iN <=57) //数字 ...

  7. 《C++游戏开发》笔记十一 平滑动画:不再颤抖的小雪花

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9430645 作者:七十一雾央 新浪微博:http:/ ...

  8. [原创]实现多层DIV叠加的js事件穿透

    Flash里面有个很好的特性是,一个容器里,不存在实际对象的部分,不会阻拦鼠标事件穿透到下一层. 前端就不一样了,两个div层叠以后,上层div会接收到所有事件(即使这个div里面内容是空的,没有任何 ...

  9. wxwidget自定义消息处理步骤

    from http://www.cppblog.com/kenlistian/archive/2009/02/06/73096.html 略有修改 wxwidget自定义消息处理步骤 自定义消息处理( ...

  10. js打开新窗口: window.open

    var iWidth = 800; var iHeight = 600; var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位 ...