[英]Removing special symbols in from python string
我試圖從給定字符串sen
中的每個單詞中刪除各種特殊符號,但我無法在 python 中找到一種方法來正確實現它。
import string
def LongestWord(sen):
maxlen = 0
count = 0
words = sen.split()
for word in words:
''.join(e for e in word if e.isalnum())
if maxlen < len(word):
maxlen = len(word)
sen = words[count]
count = count +1
return sen
# keep this function call here
print LongestWord(raw_input())
對於以下字符串:“一個美麗的句子^&!”
我得到這個作為輸出:sentence^&!
請幫助弄清楚如何刪除這些特殊符號和標點符號。
此方法返回一個字符串,它是序列 seq 中字符串的串聯。 元素之間的分隔符是提供此方法的字符串。
簡而言之,您需要將它返回的內容保存在一個變量中。
def LongestWord(sen):
words = sen.split()
answer_string = ''
for word in words:
answer_string += ''.join(e for e in word if e.isalnum())
return answer_string
print(LongestWord("a beautiful sentence^&!"))
輸出:
abeautifulsentence
只需要將''.join(...)
結果存儲在一個變量中
word = ''.join(e for e in word if e.isalnum())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.