当前分类:300例题>>正文

Python示例:去除字符串中标点符号

来源:互联网   更新时间:2023年6月20日  

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
本文固定链接:https://6yhj.com/leku-p-4369.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 红包