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.查找和替换文 ...
随机推荐
- pwd 命令
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- ntp服务及其配置
集群中使用NTP服务 简介 之前搭建zookeeper时报了一个错,我以为是ntp的问题,结果不是.这里详细学习一下如何在集群中使用ntp服务. 什么是ntp服务 来自ntp的百度百科: NTP服务器 ...
- ACE_Svc_Handler 通信原理
ACE作为通讯方面的开源架构,不但用c++实现,而且用JAVA实作的架构已经可以使用了,由此看来掌握ACE成为每歌开发通讯程序的程序员的必备技能. ACE的库分为4个层次: OS适配层该层将ACE的较 ...
- 为什么Goroutine能有上百万个,Java线程却只能有上千个?
作者|Russell Cohen 译者|张卫滨 本文通过 Java 和 Golang 在底层原理上的差异,分析了 Java 为什么只能创建数千个线程,而 Golang 可以有数百万的 Go ...
- 如何简单的实现一个tab页title的动画效果
首先我们来看看实现的效果 tab上的title沉下去的效果 先来看看布局 <?xml version="1.0" encoding="utf-8"?> ...
- c#脚本控制shader
如图所示,c#脚本控制shader颜色. public class ControlColor : MonoBehaviour { , , , ); public Material mat; publi ...
- 2820: YY的GCD
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1693 Solved: 901[Submit][Status][Discu ...
- 下周要搞大事情(ASP.NET Core & WebForms)!
下周要搞大事情(ASP.NET Core & WebForms)!
- SQLServer将一个表内指定列的所有值插入另一个表
insert into records_resolve_bak(resolve_save_addr,resolve_time,resolve_status) select resolve_save_ ...
- ShowModal 代码分析
下面为Delphi中,方法TCustomForm.ShowModal的代码,通过分析以下代码,可以了解ShowModal到底是怎么一回事! 1 2 3 4 5 6 7 8 9 10 11 12 13 ...