Python去除字符串两端空格的方法
作者:junjie 发布时间:2023-06-14 23:15:40
目的
获得一个首尾不含多余空格的字符串
方法
可以使用字符串的以下方法处理:
string.lstrip(s[, chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.
string.rstrip(s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.
string.strip(s[, chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.
具体的效果如下:
In [10]: x=' Hi,Jack! '
In [11]: print '|',x.lstrip(),'|',x.rstrip(),'|',x.strip(),'|'
| Hi,Jack! | Hi,Jack! | Hi,Jack! |
其中提供的参数chars用来删除特定的符号,注意空格并没有被移除,例如:
In [12]: x='yxyxyxxxyy Hello xyxyxyy'
In [13]: print x.strip('xy')
Hello


猜你喜欢
- 废话不多说,大家直接看代码吧!"""遗传算法实现求函数极大值—Zjh"""imp
- 使用索引提高查询速度1.前言在web开发中,页面模板,业务逻辑(包括缓存、连接池)和数据库这三个部分,数据库在其中负责执行SQL查询并返回查
- Asp(Active Server Pages)是Web服务器端脚本编写环境,可以使用Vbscript/Jscript两种脚本来编写.作为我
- session 不用多介绍,使一个http可以对应一个终端用户。session的本质使用cookie来实现。原理大概是:http 带来服务端
- 看代码吧~import torchprint(torch.__version__)补充:pytorch不同版本安装以及版本查看一:基于con
- 概念第一步:计算一个梯度 Ix,Iy第二步:整合矩阵,计算特征值第三步:比较特征值的大小第四步: 非极大值抑制,把真正的角点留下来,角点周围
- 一、弹球游戏代码 下文是tkinter的应用实例,实现弹球游戏,通过<--和-->件移动平板接球。from tkint
- 在程序中,经常需要将⼀组(通常是同为某个类型的)数据元素作为整体 管理和使⽤,需要创建这种元素组,⽤变量记录它们,传进传出函数等。 ⼀组数据
- 本文为大家分享了python2.7+selenium2实现淘宝滑块自动认证的具体代码,供大家参考,具体内容如下1.编译环境 操作系统:win
- 我就废话不多说了,大家还是直接看代码吧~import numpy as np kernel = np.array([1, 1, 1, 2])
- 简介imgaug:机器学习实验中的图像增强库,特别是卷积神经网络。支持以多种不同方式增强图像、关键点/地标、边界框、热图和分割图。安装在an
- 最近新需求来了,要给系统增加几个资源权限。尽量减少代码的改动和程序的复杂程度。所以还是使用装饰器比较科学之前用了一些登录验证的现成装饰器模块
- 平时我们获取事件对象一般写法如下:function getEvent(event) { return event
- MySQL 分区技术(是mysql 5.1以版本后开始用->是甲骨文mysql技术团队维护人员以插件形式插入到mysql里面的技术)1
- word中对于英文单词的统计也很好,大家不妨试试。如果没有安装word,而且你也是程序员的话,那么可以使用我的这段代码。通过测试,word的
- 在docker中安装mysqlubuntu官方镜像是精简的ubuntu系统,很多软件和库没有安装,所以直接安装mysql的话依赖较多,建议直
- 导语哈喽!我是木木子,今天又想我了嘛?之前不是出过一期Python美颜相机嘛?不知道你们还记得不?这一期的话话题还是围绕上期关于颜值方面来走
- 一、前言这几天宅在家里网上冲浪,无意间看到了一个比较有趣的项目,就是使用 Python 语言实现对视频中的人物的眨眼进行计数并描绘在图表中。
- 这个教程,我们将展示如何用python创建一个井字游戏。 其中我们将使用函数、数组、if条件语句、while循环语句和错误捕获等
- 前言:在网络时代,图片已经成为了我们生活中不可或缺的一部分。随着各种社交媒体的兴起,我们可以在网上看到越来越多的图片,但是如何从这些图片中获