python清除字符串中间空格的实例讲解

yipeiwu_com6年前Python基础

1、使用字符串函数replace

>>> a = 'hello world'
>>> a.replace(' ', '')
'helloworld'

看上这种方法真的是很笨。

2、使用字符串函数split

>>> a = ''.join(a.split())
>>> print(a)
helloworld

3、使用正则表达式

>>> import re
>>> strinfo = re.compile()
>>> strinfo = re.compile(' ')
>>> b = strinfo.sub('', a)
>>> print(b)
helloworld

以上这篇python清除字符串中间空格的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python对列进行平移变换的方法(shift)

在进行数据操作时, 经常会碰到基于同一列进行错位相加减的操作, 即对某一列进行向上或向下平移(shift). 往常, 我们都会使用循环进行操作, 但经过查阅相关资料, 发现结合panda...

PyQt5 pyqt多线程操作入门

首先来看一个例子: # coding=utf-8 __author__ = 'a359680405' from PyQt5.QtCore import * from PyQ...

win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程

win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程

tf2.0的三个优点: 1、方便搭建网络架构; 2、自动求导 3、GPU加速(便于大数据计算) 安装过程(概要提示) step1:安装annaconda3 step2:安装pycharm...

Python使用re模块实现信息筛选的方法

本文实例讲述了Python使用re模块实现信息筛选的方法。分享给大家供大家参考,具体如下: 背景 平时工作中,我们经常会处理大量的元数据(Raw Data),而一般的文件编辑器只能一次查...

pyqt 实现QlineEdit 输入密码显示成圆点的方法

pyqt 实现QlineEdit 输入密码显示成圆点的方法

使用自带的函数就可以实现: lineEdit.setEchoMode(QLineEdit.Password) import struct from PyQt5.QtWidgets i...