Hướng dẫn how to count number of special characters in a string in python - cách đếm số ký tự đặc biệt trong chuỗi trong python

Trong hướng dẫn này, bạn sẽ học & nbsp; cách đếm số lượng ký tự đặc biệt trong một chuỗi bằng ngôn ngữ lập trình Python.how to count the number of special characters in a string in Python programming language.

Trong bài viết trước của tôi, chúng tôi đã học: & nbsp; cách kiểm tra xem một chuỗi có chứa một ký tự đặc biệt hay không trong Python

Các nhân vật đặc biệt là những nhân vật có ý nghĩa tích hợp trong ngôn ngữ lập trình. Đây có thể là một ký tự duy nhất hoặc một tập hợp các ký tự. Thông qua ví dụ này, bạn sẽ có thể đếm số lượng ký tự đặc biệt có trong một chuỗi.

Dưới đây là một số ví dụ:

Code$Speedy
String contains 1 Special Character/s.

Code Speedy
There are no Special Characters in this String.

Để đếm các ký tự đặc biệt, chúng tôi tạo một hàm Count_special_character sẽ đếm sự xuất hiện của các ký tự đặc biệt trong một chuỗi cụ thể. Chúng tôi tạo một biến số đặc biệt và khởi tạo nó thành 0. Biến đặc biệt này được sử dụng như một bộ đếm, bất cứ khi nào có sự xuất hiện của một ký tự đặc biệt, bộ đếm này được tăng lên bởi một bộ phận.

Chương trình Python để đếm số lượng ký tự đặc biệt trong một chuỗi.

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)

Đầu ra

String contains 5 Special Character/s.

Đầu tiên, chúng tôi sử dụng cho vòng lặp để lặp qua các ký tự của chuỗi. Len (chuỗi) được sử dụng để đếm số lượng ký tự sau đó sẽ được sử dụng trong vòng lặp For làm tham số trong hàm phạm vi. Có 2 phương pháp tích hợp có sẵn trong Python:
There are 2 built-in methods available in python:

  1. Isalpha (): Phương pháp này được sử dụng để kiểm tra xem ký tự đầu vào có bảng chữ cái hay không.
  2. isDigit (): Phương thức này được sử dụng để kiểm tra xem ký tự đầu vào có phải là chữ số hay không.

Nếu các phương thức này đúng với ký tự thì câu lệnh tiếp tục được thực thi và nếu không đúng thì giá trị của Special_char được tăng thêm 1.

Cuối cùng, nếu giá trị của Special_char nhiều hơn 1 thì nó được hiển thị dưới dạng thông báo đầu ra khác thì không có ký tự đặc biệt nào trong chuỗi này được in.

Tôi muốn đếm số lượng ký tự đặc biệt [^&*$] xuất hiện trong một đoạn văn bằng Python. Bất cứ ai có thể giúp tôi làm điều này một cách ngắn gọn? Tôi không muốn sử dụng cho điều kiện vòng lặp.

Hướng dẫn how to count number of special characters in a string in python - cách đếm số ký tự đặc biệt trong chuỗi trong python

LEBELINOZ

4.82210 Huy hiệu vàng32 Huy hiệu bạc54 Huy hiệu đồng10 gold badges32 silver badges54 bronze badges

hỏi ngày 18 tháng 9 năm 2017 lúc 17:23Sep 18, 2017 at 17:23

Đội Python TeampythonPython Team

1.1973 huy hiệu vàng19 Huy hiệu bạc48 Huy hiệu đồng3 gold badges19 silver badges48 bronze badges

3

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
0 làm cho một tập hợp tất cả các ký tự đưa ra một giới hạn trên với số lần lặp.

Cho một chuỗi

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
1:

import string
import collections as ct


special_chars = string.punctuation
sum(v for k, v in ct.Counter(text).items() if k in special_chars)

Thay thế

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
2 bằng bất kỳ ký tự nào bạn muốn đếm.

Đã trả lời ngày 19 tháng 9 năm 2017 lúc 6:15Sep 19, 2017 at 6:15

Hướng dẫn how to count number of special characters in a string in python - cách đếm số ký tự đặc biệt trong chuỗi trong python

Đây có thể là một bản sao nhưng bạn có thể sử dụng regex

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
3 và len i.e thay thế tất cả các ký tự từ
#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
4 bằng
#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
5 để bạn kết thúc bằng các ký tự không từ hoặc ký tự đặc biệt.

import re
x = "asdfklsdf#$&^#@!"
new = re.sub('[\w]+' ,'', x)

Đầu ra:

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
6

len(new)
7

Trong trường hợp bạn chỉ muốn đếm những ký tự đó sau đó

new = re.sub('[^\^&*$]+' ,'', x)

Đã trả lời ngày 19 tháng 9 năm 2017 lúc 5:32Sep 19, 2017 at 5:32

Hướng dẫn how to count number of special characters in a string in python - cách đếm số ký tự đặc biệt trong chuỗi trong python

Bharath M Shettybharath M ShettyBharath M Shetty

29,2K5 Huy hiệu vàng54 Huy hiệu bạc103 Huy hiệu đồng5 gold badges54 silver badges103 bronze badges

Sử dụng ________ 17 ...

Nếu bạn có một chuỗi đầu vào của:

para = "hello #123 ^@"

Bạn có thể sử dụng

#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
7 để có được danh sách các trận đấu với
#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
9. Vì vậy, nếu chúng ta sử dụng
#Python program to count the number of 
#Special Characters in a string.

def count_special_character(string): 
  
    # Declaring variable for special characters 
    special_char= 0
   
    for i in range(0, len(string)):  
    # len(string) function to count the 
    # number of characters in given string.
      
        ch = string[i]
  
        #.isalpha() function checks whether character 
        #is alphabet or not.

        if (string[i].isalpha()):  
            continue
        
        #.isdigit() function checks whether character 
        #is a number or not.
        elif (string[i].isdigit()):
            continue
            
        else: 
            special_char += 1
            
            
    if special_char >= 1:    
        print("String contains {} Special Character/s ".format(special_char))  
    else:
        print("There are no Special Characters in this String.")
  
# Driver Code
if __name__ == '__main__' : 
    string = "Code%^&*$Speedy"
    count_special_character(string)
9: '[\ w] `, chúng ta có thể nhận được một danh sách tất cả các ký tự không đặc biệt trong chuỗi. Chỉ cần sau đó trừ độ dài của danh sách đó từ độ dài của chuỗi ban đầu để nhận số lượng ký tự đặc biệt:

specialChars = len(para) - len( re.findall('[\w]', para) )

mà trả về

String contains 5 Special Character/s.
1 :)

Đã trả lời ngày 19 tháng 9 năm 2017 lúc 6:08Sep 19, 2017 at 6:08

Hướng dẫn how to count number of special characters in a string in python - cách đếm số ký tự đặc biệt trong chuỗi trong python

Joe Iddonjoe IddonJoe Iddon

Huy hiệu vàng 19,6K77 gold badges32 silver badges51 bronze badges