Phụ âm Python

Viết chương trình Python để Đếm các Nguyên âm và Phụ âm trong một Chuỗi bằng cách sử dụng các giá trị Vòng lặp For và ASCII với một ví dụ thực tế

Chương trình Python đếm số nguyên âm và phụ âm trong một chuỗi Ví dụ 1

Chương trình python này cho phép người dùng nhập một chuỗi. Tiếp theo, nó đếm tổng số nguyên âm và phụ âm trong chuỗi này bằng vòng lặp For. Trước tiên, chúng tôi đã sử dụng Python For Loop để lặp lại từng ký tự trong Chuỗi. Trong Vòng lặp For, chúng tôi đang sử dụng Câu lệnh If để kiểm tra ký tự Chuỗi có phải là a, e, i, o, u, A, E, I, O, U. Nếu đúng, hãy tăng giá trị nguyên âm, nếu không, hãy tăng giá trị phụ âm

# Python Program to Count Vowels and Consonants in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0
consonants = 0

for i in str1:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'
       or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
        vowels = vowels + 1
    else:
        consonants = consonants + 1
 
print("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)

Python đếm nguyên âm và phụ âm trong đầu ra chuỗi

Please Enter Your Own String : Hello WOrld
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  8
>>> 
Please Enter Your Own String : Python Programs
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  12

Chương trình đếm số nguyên âm và phụ âm trong một chuỗi Ví dụ 2

Trong chương trình này, chúng ta đang sử dụng hàm lower để che chuỗi thành Chữ thường. Bằng cách này, bạn chỉ có thể sử dụng a, e, i, o, u bên trong câu lệnh If của Python (tránh viết hoa)

Python – Xóa tất cả các phụ âm khỏi chuỗi

Cải thiện bài viết

Lưu bài viết

Thích bài viết

  • Cập nhật lần cuối. 14 tháng 11 năm 2022

  • Đọc
  • Bàn luận
  • khóa học
  • Luyện tập
  • Băng hình
  • Cải thiện bài viết

    Lưu bài viết

    Đôi khi, trong khi làm việc với Python, chúng ta có thể gặp sự cố khi muốn xóa tất cả các nguyên âm không phải từ chuỗi. Đây là một câu hỏi khá phổ biến và giải pháp cho nó rất hữu ích trong lập trình cạnh tranh và lập trình hàng ngày. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
    Phương pháp #1. Sử dụng vòng lặp 
    Đây là một trong những cách thực hiện tác vụ này. Trong phần này, chúng tôi lặp qua danh sách và sau đó kiểm tra xem có nguyên âm không và lọc.
     

    Python3




    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    5

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    5
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    6
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    80

    _______581____582____22

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    84

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    87

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    88

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2____591
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    93
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    94

    Đầu ra

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    

     
    Phương pháp #2. Sử dụng khả năng hiểu danh sách 
    Đây là một trong những cách thực hiện nhiệm vụ này. Trong phần này, chúng tôi lặp qua danh sách và sau đó lọc ra các nguyên âm theo cách tương tự nhưng theo cách đơn dòng.
     

    Python3




    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    97

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    5

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    97

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    12
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    13
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    14

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    88

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2____591
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    93
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    94

    Đầu ra

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    

    Độ phức tạp Thời gian và Không gian cho tất cả các phương pháp là như nhau

    Thời gian phức tạp. Trên)

    Độ phức tạp không gian. Trên)

    Phương pháp #3. Sử dụng phương thức replace()
    Còn một cách nữa, trong khi lặp qua chuỗi nếu xảy ra phụ âm ta sẽ thay thế ký tự đó bằng ký tự trống.

    Python3




    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    5

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    36
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    9

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    8
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    41

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    0

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    44
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    5
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    6
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    44
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    00
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    02

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    81
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    3
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    06

     

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    88

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    1
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    2____591
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    4
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    93
    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    
    13

    Đầu ra

    The original string is : Gfg is best for geeks
    String after consonants removal : ieoee
    


    Ghi chú cá nhân của tôi arrow_drop_up

    Tiết kiệm

    Vui lòng Đăng nhập để nhận xét.