python3 字符串属性(一)
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']
>>> a='hello world'
>>> a.capitalize()
'Hello world'
>>> a.center()
' hello world '
>>> a='hello world'
>>> a.count('l',) >>> a.count('l',,)
>>> 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 字符串属性(一)的更多相关文章
- python3 字符串属性(四)
1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...
- python3 字符串属性(三)
maketrans 和 translate的用法(配合使用) 下面是python的英文用法解释 maketrans(x, y=None, z=None, /) Return a translation ...
- python3字符串属性(二)
1.S.isdecimal() -> bool Return True if there are only decimal characters in S, False otherwise ...
- NSAttributedString字符串属性类
//定义一个可变字符串属性对象aStr NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithStri ...
- 字符串属性使用strong的原因
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符
连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... li ...
- python3字符串
Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...
- JavaScript 常用内置对象(字符串属性、Math对象、Array数组对象)
1.字符串属性 <script> var test_var = "I Iove you"; console.log(test_var.charAt(3)) // ...
- [转]python3字符串与文本处理
转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...
随机推荐
- rm 命令简要
rm 单独使用只能删除文件不能删除文件夹 rm -r 可以删除文件夹和文件 1.rm test.txt 删除文件 2.rm -r test.txt 每次删除的时候都询问是 ...
- 出现windows启动服务失败(无法从命令行或调试器启动,需要安装InstallUtil.exe)的解决办法
两种方法1 从命令行安装2 选择项目-视图-自定义操作 然后将 安装,提交,回滚,卸载分别加入自定义操作,注意InstallClass属性为true
- IOS开发中的分享到邮件
本篇和UIWebView的全屏截图,可以一起使用,先对UIWebView进行截图,然后分享到邮箱(当时做还有分享到微信.腾讯微博.新浪微博功能,这三个根据官方资料,比较容易实现,这里就不进行解说了). ...
- Borg Maze - poj 3026(BFS + Kruskal 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9821 Accepted: 3283 Description The B ...
- 通俗的解释下音视频同步里pcr作用
PCR同步在非硬件精确时钟源的情况还是谨慎使用,gstreamer里面采用PCR同步,但是发现好多ffmpeg转的片儿,或者是CP方的片源,pcr打得很粗糙的,老是有跳帧等现象.音视频同步,有三种方法 ...
- python学习 05 函数switch功能
1.python没有switch功能,利用字典实现 如果用if else,可行但是效率不高
- cocos2d-x 2.x 支持多个方向屏幕翻转
主要改动 RootViewController.mm 的 supportedInterfaceOrientations 方法 1.四个方向 UIInterfaceOrientationMaskAll ...
- solr原理
1.solr原理: 我本人的理解:solr是为解决高性能的全文索引而出现的,它将用户输入的关键字进行智能分解,分解成一个个词,过滤掉一些多余的停词及空格等,比如,“在”.“里面”.“也”.“的”.“它 ...
- 安装android Studio和运行react native项目(跳坑篇)
1.需配环境变量,值为sdk的地址. ANDROID_HOME 值:E:\Users\HP\AppData\Local\Android\sdk 2.下载gradle-2.14.1-all.zip 包 ...
- cocos2d-x 3.0rc1 使用iconv库 解决UTF8乱码问题
多国语言要用到开源字符转换 iconv 先贴出自己的使用代码 你能够做成头文件 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include &qu ...