編寫一個 Python 程序來刪除字元串中的標點符號。我們聲明了一個可能的標點符號字元串,並使用 for 循環來迭代給定的字元串。在這個 python 示例中,if 語句檢查每個字元是否有標點符號,如果沒有找到,char 將分配給一個新的字元串,這會刪除標點符號。
# Python Program to Remove Punctuations in a String
punctuations = '''`[email protected]#$%^&*()-_=+{}[]\|;:'",.<>?'''
orgStr = "Hi!!, Welcome, Tutorial-Gateway?"
newStr = ""
for char in orgStr:
if char not in punctuations:
newStr = newStr + char
print("\nThe Original String Before Removing Punctuations")
print(orgStr)
print("\nThe Final String After Removing Punctuations")
print(newStr)
這個 Python 程序允許用戶輸入一個字元串並刪除其中的標點符號。
orgStr = input("Please Enter Any String = ")
punctuations = '''`[email protected]#$%^&*()-_=+{}[]\|;:'",.<>?'''
newStr = ""
for char in orgStr:
if char not in punctuations:
newStr = newStr + char
print("\nThe Original String Before Removing Punctuations")
print(orgStr)
print("\nThe Final String After Removing Punctuations")
print(newStr)
Please Enter Any String = [[email protected]](/cdn-cgi/l/email-protection)#&^%$# Python<>? Programs?
The Original String Before Removing Punctuations
[[email protected]](/cdn-cgi/l/email-protection)#&^%$# Python<>? Programs?
The Final String After Removing Punctuations
Learn Python Programs
Python 程序使用 while 循環刪除字元串中的標點符號。
orgStr = input("Please Enter Any String = ")
punctuations = '''`[email protected]#$%^&*()-_=+{}[]\|;:'",.<>?'''
newStr = ""
i = 0
while i < len(orgStr):
if orgStr[i] not in punctuations:
newStr = newStr + orgStr[i]
i = i + 1
print("\nThe Original String Before Removing Punctuations")
print(orgStr)
print("\nThe Final String After Removing Punctuations")
print(newStr)
Please Enter Any String = [[email protected]](/cdn-cgi/l/email-protection) tutorial @@#&^ gateway {}\ followers
The Original String Before Removing Punctuations
[[email protected]](/cdn-cgi/l/email-protection) tutorial @@#&^ gateway {}\ followers
The Final String After Removing Punctuations
hi tutorial gateway followers
原創文章,作者:N5ND3,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126619.html