如何在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教程栏。