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.查找和替换文 ...
随机推荐
- C语言include预处理命令与多文件编译
#include预处理命令几乎使我们在第一次接触C的时候就会碰到的预处理命令,可我现在还不怎么清楚,这次争取一次搞懂. 一.#include预处理指令的基本使用 预处理指令可以将别处的源代码内容插入到 ...
- torrent&BT百科
转自:百度百科 名词指代 Tracker:收集下载者信息的服务器,并将此信息提供给其他下载者,使下载者们相互连接起来,传输数据. 种子:指一个下载任务中所有文件都被某下载者完整的下载,此时下载者成为一 ...
- 【Mac + Mysql + Navicat Premium】之Navicat Premium如何连接Mysql数据库
参考文章: <mac用brew安装mysql,设置初始密码> 因为我需要连接数据库工具,需要密码,所以下面介绍如何设置.修改密码实现Navicat Premium连接Mysql数据库 建议 ...
- shell 遍历所有文件包括子目录
1.代码简单,但是难在校验,不像python那么好理解 建议在Notepad++下编辑. 2.注意引用linux命令的`是[tab]键上面那个 3.if[] 这里 Error : syntax er ...
- Dijkstra 算法——计算有权最短路径(边有权值)
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在理解 Dijkstra 的思想并用源代码加以实现: 0.2)最短路径算法的基础知识,参见 http://blog. ...
- vue实践---根据不同环境,自动转换请求的url地址
一般的项目环境分为:本地环境,测试环境,预发环境,正式环境. 这些环境的域名一般是一样的, 前端请求接口的url也会随着这些环境的变化而改变,手动修改有点麻烦,所以想个办法,让请求的地址根据域名改变而 ...
- 使用UIImageView展现来自网络的图片
本文转载至 http://www.cnblogs.com/chivas/archive/2012/05/21/2512324.html UIImageView:可以通过UIImage加载图片赋给UII ...
- 替换jar包内指定的文件
用Java jar 工具来替换. ① jar uvf test.jar test.class 把test.class 直接添加到jar包的根目录,也就是替换到根目录文件. ②jar uvf test. ...
- 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2
P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...
- WINDOWS的用户和用户组说明
1.基本用户组 Administrators 属于该administators本地组内的用户,都具备系统管理员的权限,它们拥有对这台计算机最大的控制权限,可以执行整台计算机的管理任务.内置的系统管理员 ...