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常用方法的更多相关文章

  1. python 常用方法

    在python的类中,除了常规的一些自定义函数调用之外还有一些内置函数或方法,大多数情况下不会用到,但是为了更好的学习到python类的原理也需要对其有一定的了解,下面我们一起来看一下都具体包含了那些 ...

  2. python常用方法总结

    1.os模块的路径拼接: import os now_path=os.path.abspath(__file__)#当前运行文件的路径 print(now_path) uppeer_path=os.p ...

  3. Python 常用方法和模块的使用(time & datetime & os &random &sys &shutil)-(六)

    1 比较常用的一些方法 1.eval()方法:执行字符串表达式,并返回到字符串. 2.序列化:变量从内存中变成可存储或传输到文件或变量的过程,可以保存当时对象的状态,实现其生命周期的延长,并且需要时可 ...

  4. python常用方法详解

    1,讲序列分解为单独的变量 p=(4,5) x,y=p print(x,y) 如果在分解中想丢弃某些特定的值,可以采用_来进行 data=['A','B','c','d'] _,name,age,_= ...

  5. odoo之ERP系统

    odoo大纲 第一部分:数据库postgressql 大象 第二部分:ORM(API) 第三部分:客户端 用python软件写: .py文件 包含两部分:1.自定义部分,由自己写,定义类和功能. .继 ...

  6. python浅谈正则的常用方法

    python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...

  7. python中的常用方法

    1.os模块的常用方法: >>> import os >>> >>> myFiles = ['accounts.txt', 'details.cs ...

  8. python字符串,列表,字典的常用方法

    本篇内容 字符串的常用方法 列表的常用方法 字典的常用方法 字符串的常用方法 center 字符居中显示,指定字符串长度,填充指定的填充字符 string = "40kuai" p ...

  9. 下篇:python的基本数据类型以及对应的常用方法(列表、元组、字典、集合)

    为了日后便于查询,本文所涉及到的所有命令集合如下: python中的基本数据类型有数字.字符串.布尔值.列表.元组.字典.就像每一个职业有自己特定的技能,比如医生能看病,农民能种田,每种数据类型也有属 ...

随机推荐

  1. [Java定时器]用Spring Task实现一个简单的定时器.

    今天做一个项目的的时候需要用到定时器功能.具体需求是: 每个月一号触发一次某个类中的方法去拉取别人的接口获取上一个月份车险过期的用户.如若转载请附上原文链接:http://www.cnblogs.co ...

  2. Atitit 图像处理的摩西五经attilax总结

    Atitit 图像处理的摩西五经attilax总结 1. 数字图像处理(第三版)1 2. 图像处理基础(第2版)(世界著名计算机教材精选)1 3. 计算机视觉特征提取与图像处理(第三版)2 4. Op ...

  3. Vuex2.0+Vue2.0构建备忘录应用实践

    一.介绍Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化,适合于构建中大型单页应用. ...

  4. SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...

  5. 2、C#核心编程结构

     本学习主要参考Andrew Troelsen的C#与.NET4高级程序设计,这小节主要述说以下几个东西: Hello World的Main方法: 利用VS2010新建一个控制台应用程序Hello W ...

  6. Vertica增加一个数据存储的目录

    Vertica增加一个数据存储的目录 操作语法为: ADD_LOCATION ( 'path' , [ 'node' , 'usage', 'location_label' ] ) 各节点添加目录,并 ...

  7. 读书笔记--SQL必知必会11--使用子查询

    11.1 子查询 查询(query),任何SQL语句都是查询.但此术语一般指SELECT语句. SQL还允许创建子查询(subquery),即嵌套在其他查询中的查询. 作为子查询的SELECT语句只能 ...

  8. MySQL的数据模型

    MySQL的数据类型主要分为三大类: 数值型(Numeric Type) 日期与时间型(Date and Time Type) 字符串类型(String Type) 1. 数值 MySQL的数值类型按 ...

  9. Delphi_02_Delphi程序的结构

    一.工程文件 program MultiUnit; {$APPTYPE CONSOLE} uses SysUtils, Unit1 in 'Unit1.pas'; begin //引用unit1中的变 ...

  10. source /etc/profile报错-bash: id:command is not found

    由于误操作导致 source /etc/profile 报错 -bash: id:command is not found 此时,linux下很多命令到不能能用,包括vi ls 等... 可以使用 e ...