python 字符串使用 小结
2018-03-01 17:44
211 查看
字符串在语言中的使用应该是很广泛的了吧。
使用语言不可能不学习字符串,但是字符串除了常用的方法还有其他许多的方法,这里做个小结。
一、【创建字符串】
s = '...'
s = "..."
a, b = 'test1', 'test2'
...
二、【字符串数学操作】
加:
>>> a, b = 'test1', 'test2'
>>> a+b
'test1test2'
乘:
>>> a*2
'test1test1'
%:
>>> 'test:%d' % 666
'test:666'
>>> 'test:%s' % '666'
'test:666'
>>> 'test:%r' % '666'
"test:'666'"
二、【字符串内置函数】
目前收集了这些内置方法:
三、【原始字符串】r'字符串' 或 R'字符串'
如,path = r'e:\book'
注:不能以“\”结尾四、【非字符串转换】str(obj): >>> a = 666
>>> str(a)
'666'五、【切片】(1)整个字符串>>> a = 'this is a test'>>> a[:]'this is a test'(2)正向切片>>> a[:2]'th'(3)反向切片>>> a[:-2]'this is a te'(4)倒序>>> a[::-1]'tset a si siht'(5)倒序切片>>> a[::-2]'te ish'先总结这么多吧,后面继续。
使用语言不可能不学习字符串,但是字符串除了常用的方法还有其他许多的方法,这里做个小结。
一、【创建字符串】
s = '...'
s = "..."
a, b = 'test1', 'test2'
...
二、【字符串数学操作】
加:
>>> a, b = 'test1', 'test2'
>>> a+b
'test1test2'
乘:
>>> a*2
'test1test1'
%:
>>> 'test:%d' % 666
'test:666'
>>> 'test:%s' % '666'
'test:666'
>>> 'test:%r' % '666'
"test:'666'"
二、【字符串内置函数】
目前收集了这些内置方法:
string.capitalize() | 将第一个字母大写 | |
string.count() | 获取某个字符串的数目 | |
string.find() | 获取某个字符串的起始位置 | |
string.isalnum() | 检测是否仅包含0-9A-Za-z | |
string.isalpha() | 检查是否仅包含A-Za-z | |
string.isdigit() | 检查是否仅包含数字 | |
string.islower() | 检查是否均为小写字母 | |
string.isspace() | 检查是否均为空白字符 | |
string.istittle() | 检查单词是否首字母大写 | |
string.isupper() | 检查是否均为大写字母 | |
string.join() | 连接字符串 | 将原字符串插入参数字符串中的每两个字符之间 |
string.lower() | 将字符串全部转为小写 | 对用户输入的内容进行存储时比较有用 |
string.split() | 分割字符串 | |
string.swapcase() | 将大写转为小写,小写转为大写 | |
string.tittle() | 将单词首字母大写 | 可将Abc、ABC、abc转为Abc,视为同一值 |
string.upper() | 将字符串全部转为大写 | |
len(string) | 获取字符串长度 | |
string.isdecimal() | 检查是否仅包含十进制字符 | |
string.strip() | 去掉首尾的空格 | |
string.rstrip() | 去掉末尾的空格 | |
string.lstrip() | 去掉开头的空格 | |
string.index(a) | 获取‘a’字符串在string中的第一个位置 |
如,path = r'e:\book'
注:不能以“\”结尾四、【非字符串转换】str(obj): >>> a = 666
>>> str(a)
'666'五、【切片】(1)整个字符串>>> a = 'this is a test'>>> a[:]'this is a test'(2)正向切片>>> a[:2]'th'(3)反向切片>>> a[:-2]'this is a te'(4)倒序>>> a[::-1]'tset a si siht'(5)倒序切片>>> a[::-2]'te ish'先总结这么多吧,后面继续。
相关文章推荐
- Python的字符串编码相关小结
- Python中使用中文字符串
- python字符串使用总结
- python 字符串使用总结
- python中使用状态机的简单例子---截取'/x00'之前的字符串
- Python strip lstrip rstrip使用方法(字符串处理空格)
- 使用python将用ASCII表示的16进制unicode编码的ASCII字符串转换为unicode字符串
- Python字符串join使用示例
- python使用r进行不转义字符串
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 使用python3的base64编解码实现字符串的简易加密解密
- 【python cookbook】简化字符串的translate 方法的使用
- 【python cookbook】替换字符串中的子串(使用Template)
- JavaScript 字符串处理函数使用小结
- beginning python summary chapter 3 - 使用字符串
- Python strip lstrip rstrip使用方法(字符串处理空格)
- 使用boostpython小结
- 使用Python脚本处理OC中的中文字符串
- 演示了Python中的字符串通过split分割后的使用和把分割后的字符串连接起来
- Boost.Python使用小结