您的位置:

Python 程序:去除字符串中标点符号

编写一个 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