python字符串去掉空行

There are various ways to remove spaces from a string in Python. This tutorial is aimed to provide a short example of various functions we can use to remove whitespaces from a string.

在Python中,有多种方法可以从字符串中删除空格。 本教程旨在提供各种示例的简短示例,我们可以使用这些示例来删除字符串中的空格。

Python从字符串中删除空格 (Python Remove Spaces from String)

Python String is immutable, so we can’t change its value. Any function that manipulates string value returns a new string and we have to explicitly assign it to the string, otherwise, the string value won’t change.

Python String是不可变的,因此我们无法更改其值。 任何操作字符串值的函数都将返回一个新字符串,我们必须将其显式分配给该字符串,否则,字符串值将不会更改。

Let’s say we have an example string defined as:

假设我们有一个示例字符串定义为:

s = '  Hello  World   From Pankaj \t\n\r\tHi There  '

This string has different types of whitespaces as well as newline characters.

该字符串具有不同类型的空格以及换行符。

Let’s have a look at different functions to remove spaces.

让我们看一下删除空格的不同功能。

跳闸() (strip())

Python String strip() function will remove leading and trailing whitespaces.

Python String strip()函数将删除前导和尾随空格。

>>> s.strip()
'Hello  World   From Pankaj \t\n\r\tHi There'

If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

如果只想删除前导或尾随空格,请改用lstrip()或rstrip()函数。

更换() (replace())

We can use replace() to remove all the whitespaces from the string. This function will remove whitespaces between words too.

我们可以使用replace()从字符串中删除所有空格。 此功能也将删除单词之间的空格。

>>> s.replace(" ", "")
'HelloWorldFromPankaj\t\n\r\tHiThere'

join()与split() (join() with split())

If you want to get rid of all the duplicate whitespaces and newline characters, then you can use join() function with string split() function.

如果要消除所有重复的空格和换行符,则可以将join()函数与字符串split()函数一起使用 。

>>> " ".join(s.split())
'Hello World From Pankaj Hi There'

翻译() (translate())

If you want to get rid of all the whitespaces as well as newline characters, you can use string translate() function.

如果要摆脱所有空格和换行符,可以使用字符串translate()函数 。

>>> import string
>>> s.translate({ord(c): None for c in string.whitespace})
'HelloWorldFromPankajHiThere'

Python使用正则表达式从字符串中删除空格 (Python Remove Whitespaces from String using Regex)

We can also use a regular expression to match whitespace and remove them using re.sub() function.

我们还可以使用正则表达式来匹配空格,并使用re.sub()函数将其删除。

import res = '  Hello  World   From Pankaj \t\n\r\tHi There  'print('Remove all spaces using RegEx:\n', re.sub(r"\s+", "", s), sep='')  # \s matches all white spaces
print('Remove leading spaces using RegEx:\n', re.sub(r"^\s+", "", s), sep='')  # ^ matches start
print('Remove trailing spaces using RegEx:\n', re.sub(r"\s+$", "", s), sep='')  # $ matches end
print('Remove leading and trailing spaces using RegEx:\n', re.sub(r"^\s+|\s+$", "", s), sep='')  # | for OR condition

Output:

输出:

Remove all spaces using RegEx:
HelloWorldFromPankajHiThere
Remove leading spaces using RegEx:
Hello  World   From Pankaj  Hi There
Remove trailing spaces using RegEx:Hello  World   From Pankaj   Hi There
Remove leading and trailing spaces using RegEx:
Hello  World   From Pankaj  Hi There
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: StackOverflow Question

参考: StackOverflow问题

翻译自: https://www.journaldev.com/23763/python-remove-spaces-from-string

python字符串去掉空行

python字符串去掉空行_Python从字符串中删除空格相关推荐

  1. python字符串去掉空行_从python中的字符串中删除空格

    python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...

  2. python字符串剔除空格和逗号_用逗号分隔并在Python中删除空格

    用逗号分隔并在Python中删除空格 我有一些python代码分裂逗号,但不剥离空格: >>> string = "blah, lots , of , spaces, he ...

  3. 如何使用JavaScript从字符串中删除空格?

    本文翻译自:How to remove spaces from a string using JavaScript? How to remove spaces in a string? 如何删除字符串 ...

  4. html语言中空行标记,HTML代码中的空格和空行的实例操作

    代码中空格和空行的操作方法是怎样的?我们要在代码中标记换行符或者元素标记,才能被浏览器识别为空格或者空行,现在爱站技术频道就随爱站技术频道来看看HTML代码中的空格和空行的实例操作. 例子1:(文本内 ...

  5. String中删除空格的7种方法!

    字符串,是Java中最常用的一个数据类型了.我们在日常开发时候会经常使用字符串做很多的操作.比如字符串的拼接.截断.替换等. 本文我们介绍一个比较常见又容易被忽略的一个操作,那就是移除字符串中的空格. ...

  6. [转载] python字符串数组字典_Python:字符串、列表、元组、字典

    参考链接: Python字符串| ascii_uppercase 字符串: 所有方法都修改不了字符串的值,字符串还是原值:但可以重新赋值:使用字符串方法有返回值 循环字符串的用法: 字符串方法: Na ...

  7. python里的拼接_Python拼接字符串的7种方法总结

    前言 忘了在哪看到一位编程大牛调侃,他说程序员每天就做两件事,其中之一就是处理字符串.相信不少同学会有同感. 在Python中,我们经常会遇到字符串的拼接问题,几乎任何一种编程语言,都把字符串列为最基 ...

  8. python怎么用split字符串全部分开_python实现字符串完美拆分split()的方法

    函数:split() 例子 我们想要将以下字符串rule进行拆分.字符串表示的是一个规则,由"-"得到"-".我们需要将规则中的条件属性与取值分别提取出来,存放 ...

  9. java字符串去掉中文_Java——去除字符串中的中文

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class RemoveStrChinese { priv ...

最新文章

  1. ImageNet图像数据集介绍
  2. 2022王道操作系统名词解释概念题
  3. visualSVN-server的安装图解
  4. 大学c语言程序设计期末考试试卷,大学大一c语言程序设计期末考试试卷及答案.doc...
  5. C#:winform使用chart控件绘制折线图,时间轴可缩放
  6. python将字符串拆分成单词_将字符串拆分为单词和标点符号
  7. layui单选框verify_layui 单选框选中事件
  8. html文件在echarts中,老师,echarts中所有的案例都是.html属性的文件吗?
  9. Jmeter 测试结果分析之聚合报告简介
  10. 2012-8-1复选框全选
  11. 基于android的垃圾分类识别,垃圾分类扫描识别
  12. 写给 python 程序员的 OpenGL 教程
  13. 基于AT89S52单片机的汽车LED尾灯控制器设计
  14. 码农和程序员的区别,网友:月入三万以下全是码农!
  15. CUBA Platform系列:自定义可视化组件
  16. 使用阿里云数据库RDS不得不知的注意事项
  17. python os popen_【转】python os.popen 超时问题
  18. 计算机美化标题教案,计算机基础教案标题.doc
  19. idea java 语法高亮_Intellij IDEA 中JAVA常用配置项总结
  20. 红外线发射与接收源程序

热门文章

  1. k8s 各种类型的Service讲解,及Ingress代理
  2. 抓住一切机遇,成功才有可能
  3. html css分块,web学习第二课,HTML+CSS样式+div分块
  4. 锐捷云桌面入驻天津市第四十五中学 幸福教育在云校园徜徉
  5. iOS定位权限弹框闪烁弹出后忽然消失
  6. 指静脉当前遇到的问题/展望,发展方向
  7. 服装店收银系统小程序哪家好?
  8. Terraform 基础 云计算概述 基础设施即代码
  9. 在“云基础设施即服务的魔力象限”报告中,亚马逊云科技连续三年被评为领导者
  10. android imageview 图片切换动画,模仿优酷Android客户端图片左右滑动(自动切换)效果...