Python常用方法
Python strip()方法
描述:
Python strip()方法用于移除字符串头尾指定的字符(默认为空格)。
语法:
str.strip([chars])
参数:
chars -- 移除字符串头尾指定的字符。
实例:
#!/usr/bin/python str = "0000000this is string example....wow!!!0000000";
print str.strip( '' );
运行结果:
this is string example....wow!!!
Python split()方法
描述:
Python split()通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串。
语法:
str.split(str="", num=string.count(str))
参数:
str -- 分隔符,默认为空格。
num -- 分割次数。
返回值:
返回分割后的字符串列表。
实例:
#!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print str.split( );
print str.split(' ', 1 );
运行结果:
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
Python 各种删除空格的方法:
" xyz ".strip() # returns "xyz"
" xyz ".lstrip() # returns "xyz "
" xyz ".rstrip() # returns " xyz"
" x y z ".replace(' ', '') # returns "xyz"
列表,元组,字符串之间的转化通过join(), str(), list(), tuple() 这四个函数实现。
- 用list可以把字符串和元组转化为列表
>>> demo_str = 'test'
>>> demo_tuple = ('t','e','s','t')
>>>demo_list = ['t','e','s','t']
>>> temp = list(demo_tuple)
>>> type(temp)
<type 'list'>
>>> temp = list(demo_str)
>>> type(temp)
<type 'list'>
- 用tuple() 可以将字符串和列表转化为元组
>>> demo_str = 'test'
>>> demo_tuple = ('t','e','s','t')
>>>demo_list = ['t','e','s','t']
>>> temp = tuple(demo_str)
>>> type(temp)
<type 'tuple'>
>>> temp = tuple(demo_list)
>>> type(temp)
<type 'tuple'>
- 用str() 可以将字符串和列表转化为字符串
>>> demo_str = 'test'
>>> demo_tuple = ('t','e','s','t')
>>>demo_list = ['t','e','s','t']
>>> temp = str(demo_list)
>>> type(temp)
<type 'str'>
>>> temp = str(demo_tuple)
>>> type(temp)
<type 'str'>
注意
用str()转换的字符串不能用print()函数以字符串形式显示
>>> demo_str = 'test'
>>> demo_tuple = ('t','e','s','t')
>>>demo_list = ['t','e','s','t']
>>> temp = str(demo_list)
>>> type(temp)
<type 'str'>
>>>print (temp)
['t', 'e', 's', 't']
>>> temp = str(demo_tuple)
>>> type(temp)
<type 'str'>
>>>print (temp)
('t', 'e', 's', 't')
对于这种问题要用join()函数处理
>>> demo_str = 'test'
>>> demo_tuple = ('t','e','s','t')
>>>demo_list = ['t','e','s','t']
>>> temp = ''.join(demo_list)
>>> type(temp)<type 'str'>
>>>print (temp)
test
>>> temp = ''.join(demo_tuple)
>>> type(temp)
<type 'str'>
>>>print (temp)
test
用join()和str()生成的都是字符串类型的,但为什么用print 输出的结果不同?
Python常用方法的更多相关文章
- python 常用方法
在python的类中,除了常规的一些自定义函数调用之外还有一些内置函数或方法,大多数情况下不会用到,但是为了更好的学习到python类的原理也需要对其有一定的了解,下面我们一起来看一下都具体包含了那些 ...
- python常用方法总结
1.os模块的路径拼接: import os now_path=os.path.abspath(__file__)#当前运行文件的路径 print(now_path) uppeer_path=os.p ...
- Python 常用方法和模块的使用(time & datetime & os &random &sys &shutil)-(六)
1 比较常用的一些方法 1.eval()方法:执行字符串表达式,并返回到字符串. 2.序列化:变量从内存中变成可存储或传输到文件或变量的过程,可以保存当时对象的状态,实现其生命周期的延长,并且需要时可 ...
- python常用方法详解
1,讲序列分解为单独的变量 p=(4,5) x,y=p print(x,y) 如果在分解中想丢弃某些特定的值,可以采用_来进行 data=['A','B','c','d'] _,name,age,_= ...
- odoo之ERP系统
odoo大纲 第一部分:数据库postgressql 大象 第二部分:ORM(API) 第三部分:客户端 用python软件写: .py文件 包含两部分:1.自定义部分,由自己写,定义类和功能. .继 ...
- python浅谈正则的常用方法
python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...
- python中的常用方法
1.os模块的常用方法: >>> import os >>> >>> myFiles = ['accounts.txt', 'details.cs ...
- python字符串,列表,字典的常用方法
本篇内容 字符串的常用方法 列表的常用方法 字典的常用方法 字符串的常用方法 center 字符居中显示,指定字符串长度,填充指定的填充字符 string = "40kuai" p ...
- 下篇:python的基本数据类型以及对应的常用方法(列表、元组、字典、集合)
为了日后便于查询,本文所涉及到的所有命令集合如下: python中的基本数据类型有数字.字符串.布尔值.列表.元组.字典.就像每一个职业有自己特定的技能,比如医生能看病,农民能种田,每种数据类型也有属 ...
随机推荐
- ECMASCript2015 提案 stage-3的对象展开运算符
看源码时看到如下的代码 export default { //通过mapActions将actions映射到methods里 methods: { ...mapActions([ 'updateSta ...
- 复习sql server
1.数据库的概念模型独立于具体的机器和dbms 概念模型侧重于表达建模对象之间联系的语义,它是一种独立于计算机系统的模型,是现实世界的第一层次抽象,是用户和数据库设计人员进行交流的工具 2.数据库的数 ...
- 05. Web大前端时代之:HTML5+CSS3入门系列~H5 多媒体系
Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 1.引入 概述 音频文件或视频文件都可以看做是一个容器文 ...
- FFmpeg学习1:视频解码
在视频解码前,先了解以下几个基本的概念: 编解码器(CODEC):能够进行视频和音频压缩(CO)与解压缩(DEC),是视频编解码的核心部分. 容器/多媒体文件(Container/File):没有了解 ...
- Grunt学习使用
原文地址:Grunt学习使用必看 grunt简介神马的不多说,到处一大堆. 我只说说我已经实现了的代码. 按照官方的教程 相信已经配置好了,接下来说 package.json 和 Gruntfile. ...
- GO语言下载、安装、配置
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...
- SSM项目搭建(提供源码)
1创建web动态项目,项目结构截图 2.配置日志文件 #\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B log4j.rootLogger=INFO,Console,Fi ...
- https连接的前几毫秒发生了什么
在讨论这个话题之前,先提几个问题: 为什么说https是安全的,安全在哪里? https是使用了证书保证它的安全的么? 为什么证书需要购买? 我们先来看https要解决什么问题 (手机读者推荐移步ht ...
- 『.NET Core CLI工具文档』(九)dotnet-run
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-run 翻译:dotnet-run 名称 dotnet-run -- 没有任何明确的编译或启动命令运行&q ...
- MySQL: Table 'mysql.plugin' doesn't exist的解决
安装解压版MySQL以后,不能启动,日志里面出现了这个错误: MySQL: Table 'mysql.plugin' doesn't exist 这是因为mysql服务启动时候找不到内置数据库&quo ...