• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python去除字符串两端空格的方法

Python去除字符串两端空格的方法

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python字符串去空格,python字符串去除空格,python 字符串 空格,js去除两端空格,英文word两端无空格等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

目的

  获得一个首尾不含多余空格的字符串

方法

可以使用字符串的以下方法处理:

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! |
</div>

其中提供的参数chars用来删除特定的符号,注意空格并没有被移除,例如:
In [12]: x='yxyxyxxxyy Hello xyxyxyy'

In [13]: print x.strip('xy')
 Hello
</div>

</div>

您可能想查找下面的文章:

  • Python去除字符串两端空格的方法
  • python实现指定字符串补全空格的方法
  • python实现指定字符串补全空格的方法
  • Python中用于去除空格的三个函数的使用小结
  • python中去空格函数的用法
  • Python去掉字符串中空格的方法

相关文章

  • Python标准库之循环器(itertools)介绍
  • Python3基础之基本运算符概述
  • 在Python中使用NLTK库实现对词干的提取的教程
  • python对指定目录下文件进行批量重命名的方法
  • Python中的with语句与上下文管理器学习总结
  • python定时器使用示例分享
  • 让python的Cookie.py模块支持冒号做key的方法
  • tornado捕获和处理404错误的方法
  • Python利用前序和中序遍历结果重建二叉树的方法
  • python开发环境PyScripter中文乱码问题解决方案

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 一些Python中的二维数组的操作方法
    • 在IIS服务器上以CGI方式运行Python脚本的教程
    • Python基于PycURL自动处理cookie的方法
    • Python自定义类的数组排序实现代码
    • python中元类用法实例
    • Python实现的tab文件操作类分享
    • python按照多个字符对字符串进行分割的方法
    • Python中对列表排序实例
    • Python中的条件判断语句基础学习教程
    • Python学习笔记_数据排序方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有