Python模拟删除字符串两边的空白

目标:
  1.使用string模块的whitespace
  2.删除左边、右边以及两边的空白

代码如下:

[root@localhost python]# cat rmspace.py

#!/usr/bin/env python
#coding:utf8
"""
使用字符串删除左右两端的空白。
"""from string import whitespace#删除左边的空白
def lrmsps(astr):for i in xrange(len(astr)):if astr[i] not in whitespace:return astr[i:]#当输入的全是空白字符时,返回空return ''#删除右边的空白,从列表的右边开始判断
def rrmsps(astr):for i in reversed(xrange(len(astr))):if astr[i] not in whitespace:return astr[:(i+1)]return ''#删除左右两边的空白
def rmsps(astr):return rrmsps(lrmsps(astr))if __name__ == '__main__':hi = '    hello,world.      'print '删除左边空白:|%s|' % lrmsps(hi)print '删除右边空白:|%s|' % rrmsps(hi)print '删除两边空白:|%s|' % rmsps(hi)

 

2.运行代码,测试效果

[root@localhost python]# python rmspace.py
删除左边空白:|hello,world.      |
删除右边空白:|    hello,world.|
删除两边空白:|hello,world.|

 

 

*附录:使用list的方式模拟删除字符串左右两边的空白

代码如下:

#!/usr/bin/env python
#coding:utf8
"""
使用列表的方式删除左右两端的空白。
"""
from string import whitespacedef lrmsps(astr):result = list(astr)for i in xrange(len(result)):if result[0] not in whitespace:breakresult.pop(0)return ''.join(result)def rrmsps(astr):result = list(astr)for i in xrange(len(result)):if result[-1] not in whitespace:breakresult.pop()return ''.join(result)def rmsps(astr):return rrmsps(lrmsps(astr))if __name__ == '__main__':hi = '     hello,world.    'print '|%s|' % lrmsps(hi)print '|%s|' % rrmsps(hi)print '|%s|' % rmsps(hi)

 

转载于:https://www.cnblogs.com/xkops/p/6244198.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541983.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

xml分析错误:注释未终止_错误:C中的未终止注释(无效的注释块) 常见的C程序错误...

xml分析错误:注释未终止Comments are used to write logic explain or anything that you do not want to compile. In C language there are two types of comments 1) Single line comment and 2) Multi-line comment. 注释用于编写逻辑解释或您不想编译的任何内容。 在C语言…

查看 mysql 状态_查看mysql状态的常用命令

在mysql客户端输入"show status"之后将会看到如下输出:如果想要查看某个具体的值,可以使用如下命令:show status LIKE "%具体变量%";Aborted_clients 由于客户没有正确关闭连接已经死掉,已经放弃的连接数量.A…

常用数学符号的读法及其含义

2019独角兽企业重金招聘Python工程师标准>>> 常用数学符号的读法及其含义 近来发现很多学生对一些数学符号的读法及其含义不是很清楚。今天特把一些常用的列表如下。希望能够提供一些帮助! 大写 小写 英文注音 国际音标注音 中文注音 Α…

math.atan_JavaScript中带有示例的Math.atan()方法

math.atanJavaScript | Math.atan()方法 (JavaScript | Math.atan() Method) Math.atan() is a function in math library of JavaScript that is used to find the value of arctangent of a number. Math.atan()是JavaScript数学库中的函数,用于查找数字的反正切值…

mysql 计算工作日_mysql计算工作日_MySQL

bitsCN.commysql计算工作日Sql代码 DELIMITER $$ drop procedure if exists pGetWorkDays$$ create procedure pGetWorkDays(s datetime,e datetime) begin select floor(days/7)*5days%7 -case when 6 between wd and wddays%7-1 then 1 else 0 end -case when 7 between wd a…

后端码农谈前端(HTML篇)第三课:常见属性

一、HTML全局属性 1、核心属性 属性描述id设置元素的唯一 id。class设置元素的一个或多个类名(引用样式表中的类)。style设置元素的行内样式(CSS内联样式)。title设置有关元素的额外信息(可在工具提示中显示&#xff0…

mysql数据库的服务无法启动_mysql5数据库服务无法启动

mysql5数据库服务无法启动我最近安装了MySQL 5.0 Community Edition因为听很多人介绍,mysql开源,免费,速度快,于是终于按捺不住,尝试一下。谁知这一尝试,噩梦就来了。[安装环境]windows xp sp2MySQL 5.0 Co…

math.asin_JavaScript中带有示例的Math.asin()方法

math.asinJavaScript | Math.asin()方法 (JavaScript | Math.asin() Method) Math.asin() is a function in math library of JavaScript that is used to find the value of arcsine of a number and return the value in radians. Math.asin()是JavaScript数学库中的函数&…

POJ 3422 费用流

思路&#xff1a; 把每个方块拆成两个点 1个入点 1个出点 当前格子的入->出连费用-w[i][j] 容量1的边 当前格子的入->出连费用0 容量k-1的边 此格子的出向右&下&#xff08;如果有的话&#xff09;的格子的入连费用0容量k的边 //By SiriusRen #include <queu…

mysql内置变量_MySQL常用内置变量

MySQL用很多常用的内置变量&#xff0c;掌握这些内置变量后对于我们快速获取当前MySQL的配置有很大帮助&#xff0c;下面就来列举几个常用的变量。查看当前MySQL版本号信息。show variables like version;MariaDB [(none)]> show variables like version;------------------…

Android-TextView跑马灯效果

要实现跑马灯还是比较简单的。 同时有几个需要注意的点&#xff0c;先上代码&#xff1a; 1 public class MTView extends TextView {2 3 4 public MTView(Context context) {5 super(context);6 }7 8 public MTView(Context context, AttributeSet attrs)…

qt没有mysql文件夹_qt5-qt目录下没有mysql文件夹

我安装的qt5.2.1&#xff0c;为什么我的qt安装目录下没有 (例如&#xff1a;D:\Qt\Qt5.0.1\Sources\qtbase\src\plugins\sqldrivers\mysql\)mysql这个文件夹&#xff1f;无法Create MySQL driver for Qt5 on WindowsWindows下解决QSqlDatabase:QMySQL driver not loaded解决参考…

Linux Shell命令能力倾向问题和解答

This section contains Aptitude Questions and Answers on Linux Shell Commands. 本节包含有关Linux Shell命令的 Aptitude问答。 1) Which of the following command is used to check a Linux command is a built-in shell command or external command? cmdtypetypectyp…

php mysql 权重_PHP对MySql的常用操作

关于PHP对MySql的常用操作最近做网站&#xff0c;用PHP操作数据库也很多次了&#xff0c;但总是忘记&#xff0c;参考了网上的很多资料&#xff0c;算是整理记录下。数据库操作类实现数据库的连接&#xff0c;断开&#xff0c;以及请求&#xff1a;/*** Created by PhpStorm.* …

Python timedelta total_seconds()方法与示例

Python timedelta.total_seconds()方法 (Python timedelta.total_seconds() Method) timedelta.timedeltotal_seconds() method is used in the timedelta class of module datetime. timedelta.timedeltotal_seconds()方法在模块datetime的timedelta类中使用。 It uses an i…

static和extern对函数的作用

2019独角兽企业重金招聘Python工程师标准>>> 外部函数&#xff1a;定义的函数能被本文件和其他文件访问 默认情况下所有函数都是外部函数 不允许有同名的外部函数内部函数&#xff1a;定义的函数只能被本文件访问&#xff0c;其他文件不能访问 允许不同文件中有同名…

MySQL从服务器写入报错吗_MySQL主从复制读写分离及奇怪的问题

一直都没有写blog的习惯&#xff0c;以前总觉得自己的脑子就是最好的记忆容器&#xff0c;现在觉得我好像有个假脑子。当时是使用阿里云镜像&#xff0c;安装了两台ECS&#xff0c;结果配置MySQL的时候出现了UUID重复问题。先从配置主从开始吧&#xff0c;值得记录。文中很多部…

python二分法查找程序_查找Python程序的输出| 套装2(基础)

python二分法查找程序Program 1: 程序1&#xff1a; a 10b 3res a/b print "a/b: ", res res float(a/b)print "float (a/b) : ", res res float (a) /float (b)print "float (a/b) : ", res res a/b print "a/b: ", resOutput…

数据库 数据库SQL语句一

字符和日期 --字符和日期都要包含在单引号中 --字符大小写敏感&#xff0c;日期格式敏感 --默认的日期格式是DD-MON-RR--查询当前系统时间 SQL> select sysdate from dual; --查询工资在1000~2000之间的员工信息 SQL> select * from emp where sal>1000 and sal<20…

mysql text保存图片_用mysql 如果包含有文字和图片,那么我要用哪种数据类型存储呢?还是分开,用TEXT和BLOB吗?...

rootytt:/var/lib/mysql-files# for i in seq 1 100; do cp 微信图片_20190711095019.jpg "$i".jpg;done;rootytt:/var/lib/mysql-files# ls100.jpg 17.jpg 25.jpg 33.jpg 41.jpg 4.jpg 58.jpg 66.jpg 74.jpg 82.jpg 90.jpg 99.jpg f8.tsv10.jpg 18…