python string
string比较连接
>>> s1="python string"
>>> len(s)
13 >>> s2=" python string2 "
>>> s=s1+s2
>>> s
'python string python string2 '
>>> >>> cmp(s1,s2)
1
string 截取
>>> s1[0]
'p'
>>> s1[-1]
'g' >>> s1[0:5]
'pytho' >>> s1[1:]
'ython string' >>> s1[::-1]
'gnirts nohtyp' >>> s1
'python string' >>> s1[2:-1]
'thon strin' >>> s1[:8:-1]
'gnir' >>> s1[::2]
'pto tig'
>>> s1[::-2]
'git otp'
>>> s1[5::-2]
'nhy'
>>> s1[:5:-2]
'git '
string搜索和替换
>>> s3="onnoonssss"
>>> s3.count("s")
4
>>> s3.count("ss")
2
>>> s3.count("on")
2
>>> s3.count("no")
1 >>> s4="sssnnnsss"
>>> s4.index("s")
0
#rindex 从右边算起的第一次出现的也就是index的逆序 >>> s4.rindex("s")
8 >>> s4.index("s",4)
6
>>> s4.index("n")
3
>>> s4.index("n",4)
4
#如果搜索的字符没有,则报错
>>> s4.index("g")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s4.find("s")
0
>>> s4.find("s",5)
6
#如果搜索的字符没有则-1
>>> s4.find("g",5)
-1 >>> s4.replace("s","g")
'gggnnnggg'
>>> s4.replace("ss","g")
'gsnnngs' #S.replace(oldstr, newstr, [count]) 把S中的oldstr替换为newstr,count为替换次数 >>> s4.replace("s","g",1)
'gssnnnsss'
>>> s4.replace("s","g",2)
'ggsnnnsss' >>> s2
' python string2 '
>>> s2.strip()
'python string2'
>>> s2.lstrip()
'python string2 '
>>> s2.rstrip()
' python string2'
>>> s="* python * * string *"
>>> s.strip("*")
' python * * string '
>>> s.lstrip("*")
' python * * string *'
string分割组合
#S.split([sep, [maxsplit]]) #以sep为分隔符,把S分成一个list。maxsplit表#示分割的次数。默认的分割符为空白字符
>>> s2
' python string2 '
>>> s2.split(" ")
['', '', 'python', 'string2', ''] >>> s2.split(" ",1)
['', ' python string2 '] >>> s2.rsplit(" ",1)
[' python string2', ''] #S.splitlines([keepends]) #把S按照行分割符分为一个list,keepends是#一个bool值,如果为真每行后而会保留行分割符。 >>> s5="""
... first line
... second line
... """
>>> s5.splitlines()
['', 'first line', 'second line'] #str.join(sequence) sequence -- 要连接的元素序列 返回通过指定字符
#连接序列中元素后生成的新字符串。
>>> "-".join(s1)
'p-y-t-h-o-n- -s-t-r-i-n-g'
string测试
>>> s
'python string python string2 ' >>> s1.isupper()
False
>>> s1.islower()
True
#是否全是空白字符,并至少有一个字符
>>> s1.isspace()
False
#是否全是数字,并至少有一个字符
>>> s.isdigit()
False
#是否全是字母,并至少有一个字符
>>> s.isalpha()
False
#是否全是字母和数字,并至少有一个字符
>>> s.isalnum()
False
#是否是首字母大写的
>>> s.istitle()
False #str.startswith(str, beg=0,end=len(string));
>>> s
' Python string !! '
>>> s.startswith(" P")
True
>>> s.endswith(" ")
True
>>> s.startswith("P",2)
True
>>> s.startswith("P",3)
False
>>>
string输出对齐
#输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格
>>> s.ljust(50,"s")
' Python string !! sssssssssssssssssssssssssssss'
#右对齐
>>> s.rjust(50,"s")
'sssssssssssssssssssssssssssss Python string !! '
#中间对齐
>>> s.center(50,"c")
'cccccccccccccc Python string !! ccccccccccccccc'
#把S变成width长,并在右对齐,不足部分用0补足
>>> s.zfill(50)
'00000000000000000000000000000 Python string !! '
string大小写
>>> s1.lower()
'python string'
>>> s1.upper()
'PYTHON STRING'
#大小写互换
>>> s1.swapcase()
'PYTHON STRING'
>>> s1.capitalize()
'Python string'
>>> s1.title()
'Python String'
python string的更多相关文章
- python string module
String模块中的常量 >>> import string >>> string.digits ' >>> string.letters 'ab ...
- The internals of Python string interning
JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...
- Python string objects implementation
http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects impleme ...
- Python string replace 方法
Python string replace 方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...
- Python string interning原理
原文链接:The internals of Python string interning 由于本人能力有限,如有翻译出错的,望指明. 这篇文章是讲Python string interning是如何 ...
- python string与list互转
因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string. >>&g ...
- python string 文本常量和模版
最近在看python标准库这本书,第一感觉非常厚,第二感觉,里面有很多原来不知道的东西,现在记下来跟大家分享一下. string类是python中最常用的文本处理工具,在python的 ...
- Python String 方法详解
官网文档地址:https://docs.python.org/3/library/stdtypes.html#string-methods 官网 公号:软测小生ruancexiaosheng 文档里的 ...
- python string/list转换
python的read.write方法的操作对象都是string.输入.输出和逻辑业务上很多时候都要用到string.list互转. 1.简单用法 import stringstr = 'abcde' ...
随机推荐
- myeclipse maven pom.xml 配置错误
http://www.oschina.net/question/2265006_219341#tags_nav maven pom.xml 配置文件错误 腾讯云消息队列CMQ架构解析> ...
- putty自动登录
如果没有公钥/密钥对,就用 PuTTYgen 创建一个,已经有了就可以忽略这一步.一个公钥/密钥对可以用在不同的服务器上,所以也不需要重复创建,关键要有足够强健的密码和安全的存放. 象先前一样输入帐户 ...
- Yii2框架安装(windows)
-->安装PHP环境Wamp集成环境,XAMMP等.-->安装Composerhttp://pan.baidu.com/s/1i3fejjvPS:安装过程中的有一个手动操作项选择php.e ...
- VB6.0对鼠标滚轮不支持的解决方法[转]已经测试work
今天要修改一个老DLL文件,安装了vb6,用起来很不爽. VB6编辑器 和 VBA编辑器 (Office 中的VB编辑器)都不支持鼠标滚动. 但 MS 已经提供了补丁http://download.m ...
- linux hugepage
The intent of this file is to give a brief summary of hugetlbpage support inthe Linux kernel. This ...
- JS如何判断包括IE11在内的IE浏览器 <转载>
今天碰到一个奇怪的问题,有一个页面,想指定用IE浏览器打开,在VS开发环境没有问题,但部署到服务器上,即使是用IE打开页面,还是提示“仅支持IE”,真是晕啊!! 判断是否IE浏览器用的是window. ...
- 如何使用 Quagga BGP(边界网关协议)路由器来过滤 BGP 路由
在之前的文章中,我们介绍了如何使用 Quagga 将 CentOS 服务器变成一个 BGP 路由器,也介绍了 BGP 对等体和前缀交换设置.在本教程中,我们将重点放在如何使用前缀列表prefix-li ...
- 如何使用 WinInet 时提供下载上载进度信息
概要许多开发人员都使用 WinInet 函数来下载或上载文件在 Internet 上的想要提供一个进度条以指示多少文件传输已完成,但多少就越长.您可以使用以下机制来完成此.Collapse image ...
- 使用excel快速制表 拒绝粗心
办公室打印个表格 使用了word打印后 发现 id重复很多 只好网上找了点excel 2003资料 学习小 快速制作表格 新建一个excel文件. 在新建excel中,用鼠标选中需要的表格行数列数,然 ...
- 安装EditPlus
生成注册码:http://www.jb51.net/tools/editplus/ 862C2-DABE8-E30AA-CCE33-65E69