split python如何去掉字符串的句號和逗號
4 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
不需要用split啊,split是分割
用replace(src, des)#des為需要替換成的目標字串,src為需要替換的源字串
如:
>>> str = "My name is xxx, and yours? ..."
>>> print str
My name is xxx, and yours? ...
>>> str = str.replace(',','')
>>> print str
My name is xxx and yours? ...

叮當貓咪
TA貢獻1776條經驗 獲得超12個贊
123 | ' '.join(' input a ,b,c,d;'.split()[ 1 :]) # 'a,b,c,d;' |
其實這種問題,你也可以完全使用。
1 | 'input a ,b,c,d;' .raplace( 'input' , ' ').replace(' output ', ' ') |
這種去替換
但是你描述的還是不是太清楚
或者你可以將第一個元素選擇出來,然后再替換
123 | 'input a ,b,c,d;' .replace( 'input a ,b,c,d;' .split()[ 0 ], '') # 結果: ' a ,b,c,d;' |

冉冉說
TA貢獻1877條經驗 獲得超1個贊
12 | import re print (re.sub(r '^\w+\s*' , ' ', ' input a, b c d', count = 1 )) |
正則很適合做這種事情.
這個是刪掉了頂格起的第一個單詞及其右側緊挨著的所有白空格(如果有的話)
添加回答
舉報
0/150
提交
取消