python编程怎样去掉空格

您所在的位置:网站首页 python输出怎么去掉空格 python编程怎样去掉空格

python编程怎样去掉空格

2022-05-12 21:12| 来源: 网络整理| 查看: 265

去掉所有空格:

有两种方式

eg1:调用字符串的替换方法把空格替换成空

string = " * it is blank space test * "

str_new = string.replace(" ", "")

print str_new

result:

*itisblankspacetest*

eg2:正则匹配把空格替换成空

import re

string = " * it is blank space test * "

str_new = re.sub(r"\s+", "", string)

print str_new

result:

*itisblankspacetest*



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3