Python数据类型String字符串
2017-06-26 19:47
459 查看
1、转义字符
2、字符串运算符
操作符 | 描述 | 例:a=”hello” |
---|---|---|
+ | 字符串连接 | a + ” world” #hello world |
* | 重复输出字符 | a * 2 #hellohello |
[] | 通过索引(0开始)获取字符串中字符 | a[0] #h |
[ : ] | 截取字符串指定索引的字符 | a[0:5] #hello |
in | 字符串包含指定字符返回True反之False | “o” in a #True |
not in | 字符串不包含指定字符返回True反之False | “o” in a #False |
r/R | 所有的字符串都是直接按照字面的意思来使用 | r’\n’ #\n |
3、字符串格式化
4、Unicode 字符串
定义:u’hello’5、字符串内建函数
操作符 | 描述 | 例:str=”hello world” |
---|---|---|
str.replace(old,new,count) | 将字符串中旧字符串替换成新字符串,替换次数 | str.replace(‘l’,’o’,1) #heolo world |
str.split(str,count) | 指定分隔符指定分割词素返回字符串列表 | str.split(’ ‘,1) #[‘hello’, ‘world’] |
str.partition(str) | 指定分隔符将字符串进行分隔成元组 | str = “hello/world” str.partition(‘/’) #(‘hello’, ‘/’, ‘world’) |
str.find(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定字符第一个的索引位置 | str.find(‘o’,5,10) #7 |
str.rfind(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定字符最后一个的索引位置 | str.rfind(‘l’) #9 |
format | 格式化字符串 | “{0} {1}”.format(“hello”,”world”) #hello world |
str.index(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定字符第一个的索引位置 | str.index(‘o’,5,10) #7 |
str.rindex(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定字符最后一个的索引位 | str.rindex(‘o’,5,10) #7 |
str.count(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定字符次数 | str.count(‘h’,0,10) #1 |
str.encode(encoding,errors) | 以指定编码格式编码字符串,不同错误处理方案默认strict | str.encode(‘base64’,’strict’) #aGVsbG8gd29ybGQ= |
str.decode(encoding,errors) | 以指定编码格式解码字符串,不同错误处理方案默认strict | str=”aGVsbG8gd29ybGQ=” str.decode(‘base64’,’strict’) #hello world |
str.join(连接的序列) | 将序列中的元素以指定的字符连接生成一个新的字符串 | str=”&” list = (“a”,”b”,”c”) str.join(list) #a&b&c |
str.startswith(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定前缀字符返回True,反之False | str.startswith(‘h’,0,11) #True |
str.endswith(str,start,stop) | 指定字符串开始和结束搜索位置搜索指定后缀字符返回True,反之False | str.endswith(‘d’,0,11) #True |
str.isupper() | 检测字符串中所有字母是否都为大写,是返回True反之False | False |
str.istitle() | 检测字符串中所有单词拼写首字母是否为大写,且其他字母为小写,是返回True反之False | False |
str.lower() | 转换字符串中所有大写字符为小写 | hello world |
str.upper() | 转换字符串中所有小写字符为大写 | HELLO WORLD |
str.capitalize() | 将字符串的第一个字母变成大写,其他字母变小写 | Hello world |
str.isspace() | 检测字符串是否只由空格组成,是返回True反之False | True |
str.islower() | 检测字符串是否由小写字母组成,是返回True反之False | True |
str.isalpha() | 检测字符串是否由字母组成,是返回True反之False | False带空格了 |
str.isdigit() | 检测字符串是否只由数字组成,是返回True反之False | False |
str.isnumeric() | 检测unicode字符串是否只由数字组成,是返回True反之False | False |
u”hello”.isdecimal() | 检查unicode字符串是否只包含十进制字符,是返回True反之False | False |
str.isalnum() | 检测字符串是否由字母和数字组成,是返回True,反之False | False |
str.center(strlength,fillstr) | 原字符居中,填充字符填充至总长度 | str.center(15,’m’) #mmhello worldmm |
str.ljust(newlen,str) | 原字符串左对齐使用指定字符填充至指定长度的新字符串 | str.ljust(15,’0’) #hello world0000 |
str.rjust(newlen,str) | 原字符串右对齐使用指定字符填充至指定长度的新字符串 | str.rjust(15,’0’) #0000hello world |
str.zfill(newlen) | 原字符串右对齐使用0填充至指定长度的新字符串 | str.rjust(15,’0’) #0000hello world |
min(str) | 字符串中最小的字母 | 空格 |
max(str) | 字符串中最大的字母 | w |
str.expandtabs(x) | 把字符串中\t转为x个空格 | str=”hello\tworld” str.expandtabs(3) #hello world |
str.rstrip(str) | 删除末尾指定字符 | str.rstrip(‘d’) #hello worl |
str.strip(str) | 删除字符串头尾指定字符 | str.strip(‘d’) #hello worl |
相关文章推荐
- Python 中的基本数据类型 ---Number(数字) 和 String(字符串)
- Python基础数据类型-字符串(string)
- 4-Python3从入门到实战—基础之数据类型(字符串-String)
- JSON: Python Objects与String之间转换【json.dumps把python任意数据类型转换成字符串,json.loads把字符串转换为unicode编码】
- Python基础:数据类型-字符串:string
- 走进Python世界(五)数据类型 2. 字符串(String)
- Python中将字符串类型转为字典类型(string to dict)
- Python数据类型——序列(字符串,列表,元组)
- python3手册-基本数据类型-字符串
- 【C#】解决进行反序列化时出错:。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。
- PHP基础之数据类型4——字符串(String)
- Javascript基础教程之数据类型 (字符串 String)
- Python学习-5.Python的变量与数据类型及字符串的分割与连接
- Python核心数据类型——字符串
- Python数据类型之数字类型、字符串类型
- Python核心数据类型——字符串(1)
- Python数据类型 -- 字符串
- Javascript基础系列之(三)数据类型 (字符串 String)
- Python基础(3) - 数据类型:2字符串类型
- String、字符串缓冲区、基本数据类型包装类