Hướng dẫn how do you remove punctuation from a string using string punctuation in python? - làm cách nào để xóa dấu câu khỏi chuỗi bằng cách sử dụng dấu câu chuỗi trong python?

Nhiều lần khi làm việc với các chuỗi Python, chúng tôi có một vấn đề trong đó chúng tôi cần loại bỏ một số ký tự nhất định khỏi chuỗi. Điều này có thể có các ứng dụng trong tiền xử lý dữ liệu trong lĩnh vực khoa học dữ liệu và cả trong lập trình hàng ngày. Hãy để thảo luận về những cách nhất định mà chúng ta có thể thực hiện nhiệm vụ này bằng Python.

Phương pháp 1: Xóa dấu câu từ chuỗi có dịch

Hai đối số đầu tiên cho phương thức String.Translate là các chuỗi trống và đầu vào thứ ba là danh sách python của dấu câu cần được xóa. Điều này hướng dẫn phương pháp Python để loại bỏ dấu câu từ một chuỗi. Đây là một trong những cách tốt nhất để dải dấu câu từ một chuỗi.best ways to strip punctuation from a string.

Python3

import string

test_str =

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0

test_str =

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
3

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
5
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
6
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
9

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
1

Output:

Gfg is best for  Geeks 

Phương pháp 2: Xóa dấu câu từ một chuỗi với vòng lặp PythonPython loop

Đây là cách vũ phu trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi kiểm tra các dấu chấm câu bằng cách sử dụng một chuỗi thô có chứa dấu chấm câu và sau đó chúng tôi xây dựng một chuỗi loại bỏ các dấu câu đó.

Python3

test_str =

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
5
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
9

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
0=

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
2
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
5

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
0

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
1test_str __
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
4

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
5
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
9

Output: 

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 

Phương pháp 3: Xóa dấu câu từ một chuỗi với Regex & NBSP; 

Phần của việc thay thế bằng dấu câu cũng có thể được thực hiện bằng Regex. Trong đó, chúng tôi thay thế tất cả các dấu câu bằng một chuỗi trống bằng cách sử dụng một regex nhất định.

Python3

import

Hello he said and went
1

test_str =

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
5
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
9

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
2
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
5

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
0

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
1test_str __
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
4

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 

The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 0The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 5# define punctuation punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' my_str = "Hello!!!, he said ---and went." # To take input from the user # my_str = input("Enter a string: ") # remove punctuation from the string no_punct = "" for char in my_str: if char not in punctuations: no_punct = no_punct + char # display the unpunctuated string print(no_punct) 7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 9

Python3

test_str =

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
5
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
9

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
0=

test_str 0=test_str 2

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
2
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
5

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
0

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
1test_str 0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8==7

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
4
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
3
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
4
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
0

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
1test_str __
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
4

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
0
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
5
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
7
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
8
The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 
9

Phương pháp 3: Xóa dấu câu từ một chuỗi với Regex & NBSP;O(n)

Phần của việc thay thế bằng dấu câu cũng có thể được thực hiện bằng Regex. Trong đó, chúng tôi thay thế tất cả các dấu câu bằng một chuỗi trống bằng cách sử dụng một regex nhất định.O(n)


Đôi khi, chúng ta có thể muốn chia một câu thành một danh sách các từ.

Trong những trường hợp như vậy, trước tiên chúng ta có thể muốn làm sạch chuỗi và xóa tất cả các dấu chấm câu. Dưới đây là một ví dụ về cách nó được thực hiện.

Mã nguồn

# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)

Đầu ra

Hello he said and went

Trong chương trình này, trước tiên chúng tôi xác định một chuỗi các dấu chấm câu. Sau đó, chúng tôi lặp lại chuỗi được cung cấp bằng cách sử dụng vòng lặp

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter :  Gfg is best  for  Geeks 
2.

Trong mỗi lần lặp, chúng tôi kiểm tra xem ký tự có phải là dấu chấm câu hoặc không sử dụng bài kiểm tra thành viên không. Chúng tôi có một chuỗi trống mà chúng tôi thêm (concatenate) ký tự nếu nó không bị dấu câu. Cuối cùng, chúng tôi hiển thị chuỗi được làm sạch.

Làm thế nào để bạn loại bỏ dấu câu từ xử lý chuỗi?

Ví dụ về việc loại bỏ puncuatioin khỏi chuỗi bằng cách sử dụng thay thế () trong mã bên dưới, chúng tôi đang gọi phương thức thay thế () với biểu thức regex sẽ tìm thấy tất cả các ký tự có dấu câu có trong chuỗi và nó sẽ thay thế bằng một chuỗi trống ("") mà trông giống như dấu chấm câu được gỡ bỏ.replaceAll() In the below code, we are calling replaceAll() method with regex expression that will find all the characters that are punctuation present in the string and it will replace with an empty string( "" ) which looks like punctuations are removed.

Làm cách nào để loại bỏ các ký tự đặc biệt khỏi một chuỗi trong Python?

Các phương thức sau có thể được sử dụng để loại bỏ các ký tự đặc biệt khỏi chuỗi trong Python,..
Phương thức isalnum () ..
Sử dụng các biểu thức chính quy (regex) trong Python ..
Phương thức thay thế () ..
Phương thức bộ lọc () ..
Phương thức dịch () ..

Dấu chấm câu làm gì trong Python?

Trong Python, chuỗi.Dấu câu sẽ cung cấp cho tất cả các bộ chấm câu.Tham số: Không lấy bất kỳ tham số nào, vì nó không phải là một hàm.Trả về: Trả về tất cả các bộ chấm câu.give the all sets of punctuation. Parameters : Doesn't take any parameter, since it's not a function. Returns : Return all sets of punctuation.

Làm cách nào để loại bỏ dấu câu trong Python NLP?

Để loại bỏ dấu câu, bạn có thể sử dụng biểu thức thông thường hoặc hàm isalnum () của Python.Nó hoạt động: >>> 'với dấu chấm.'.dịch (không có, chuỗi.use a regular expression or python's isalnum() function. It does work: >>> 'with dot. '. translate(None, string.