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ả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 : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee80
_______581____582____22
The original string is : Gfg is best for geeks String after consonants removal : ieoee84
The original string is : Gfg is best for geeks String after consonants removal : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee87
The original string is : Gfg is best for geeks String after consonants removal : ieoee88
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2____591
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee93
The original string is : Gfg is best for geeks String after consonants removal : ieoee94
Đầ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 : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee97
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee97
The original string is : Gfg is best for geeks String after consonants removal : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee12
The original string is : Gfg is best for geeks String after consonants removal : ieoee13
The original string is : Gfg is best for geeks String after consonants removal : ieoee14
The original string is : Gfg is best for geeks String after consonants removal : ieoee88
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2____591
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee93
The original string is : Gfg is best for geeks String after consonants removal : ieoee94
Đầ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 : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee36
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee9
The original string is : Gfg is best for geeks String after consonants removal : ieoee8
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee41
The original string is : Gfg is best for geeks String after consonants removal : ieoee0
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee5
The original string is : Gfg is best for geeks String after consonants removal : ieoee6
The original string is : Gfg is best for geeks String after consonants removal : ieoee44
The original string is : Gfg is best for geeks String after consonants removal : ieoee00
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee02
The original string is : Gfg is best for geeks String after consonants removal : ieoee81
The original string is : Gfg is best for geeks String after consonants removal : ieoee2
The original string is : Gfg is best for geeks String after consonants removal : ieoee3
The original string is : Gfg is best for geeks String after consonants removal : ieoee06
The original string is : Gfg is best for geeks String after consonants removal : ieoee88
The original string is : Gfg is best for geeks String after consonants removal : ieoee1
The original string is : Gfg is best for geeks String after consonants removal : ieoee2____591
The original string is : Gfg is best for geeks String after consonants removal : ieoee4
The original string is : Gfg is best for geeks String after consonants removal : ieoee93
The original string is : Gfg is best for geeks String after consonants removal : ieoee13
Đầ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