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. Oracle直接路径加载--append的深度解析

    ㈠ 直接路径加载和buffer cache              直接路径插入的数据不经过buffer cache,从PGA直接把数据格式化成Oracle块       然后由普通的Oracle ...

  2. 第三篇:Logstash 安装配置

    Logstash 简介: Logstash 是一个实时数据收集引擎,可收集各类型数据并对其进行分析,过滤和归纳.按照自己条件分析过滤出符合数据导入到可视化界面.Logstash 建议使用java1.8 ...

  3. win10 下eclipse tomcat 热部署问题?

    前言: 问题的描述: 用的环境是maven,java,tomcat,win10 tomcat server配置如下 项目发布之后,修改jsp,报错,错误详情如下: 解决办法.勾选server opti ...

  4. COM组件多接口对象模型

    COM组件有两种接口类型,Dual and Custom,如下图所示.本文说的是Custom.所谓多接口COM对象是指此COM对象实现了多于一个的自定义接口,即Custom接口. 接口图如下: 需要注 ...

  5. cnn 实例

    http://www.geekcome.com/content-10-3761-1.html http://www.geekcome.com/content-10-3761-1.html http:/ ...

  6. linux 常用的17个性能指标

    1.Average load:Average number of processes simultaneously in Ready state during the last minute.   上 ...

  7. springMVC的注释集合

    SpringMVC的工作原理 主要核心实现是DispatcherServlet. 一般来讲客户端对服务器发送请求,是由DispatcherServlet控制的,DispatcherServlet接受到 ...

  8. 数据处理 数据入数据库 与 Excel

    Python  数据处理   中间数据 Excel   团队交流分工   低的沟通成本    数据入数据库 如postgresql

  9. 【python】-- 函数、无参/有参参数、全局变量/局部变量

    函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函 ...

  10. PoC简介

    无线一键通功能,POC(PTT Over Cellular)也称PTT(Push To Talk)功能.PTT:一键通(Push-to-Talk)功能是一种全新的移动技术,可以快速地进行"一 ...