首页 > 编程知识 正文

python去掉字符串中的空格,python怎么去掉字符串中的空格

时间:2023-05-06 00:40:14 阅读:178577 作者:1799

如何在python中删除字符串中空字符:

1、replace (使用函数

replace (使用函数将所有空格('')替换为) () defremove(string ) :

returnstring.replace (',' );

string='HELLO!' ;

打印(原始字符串)字符串);

print ('

新字符串: ' remove(string );

输出:

2、使用split (函数join )函数

split ) )函数通过指定分隔符对字符串进行切片,并返回拆分字符串中所有单词的列表。 然后,使用join (函数重复连接这些字符。 efremove(string ) :

return''.join(string.split ) );

string='world ';

打印(原始字符串)字符串);

print ('

新字符串: ' remove(string );

执行结果如下。

更多Python知识请关注云海天Python教程栏。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。