Làm thế nào để bạn hiển thị phụ âm trong 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. Bên 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 Program to Count Vowels and Consonants in a String

str1 = input["Please Enter Your Own String : "]
vowels = 0
consonants = 0
str1.lower[]

for i in str1:
    if[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]

ví dụ.   

Input : x = 'c'
Output : Consonant

Input : x = 'u'
Output : Vowel

Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp

Chúng tôi kiểm tra xem ký tự đã cho có khớp với bất kỳ nguyên âm nào trong số 5 nguyên âm không. Nếu có, chúng tôi in "Nguyên âm", nếu không, chúng tôi in "Phụ âm".  

Python3




# Python3 program to check if a given

# character is vowel or consonant.

 

Consonant
Vowel
0

Consonant
Vowel
1

Consonant
Vowel
2
Consonant
Vowel
3

 

Consonant
Vowel
4_______4_______5
Consonant
Vowel
6
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
9
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
4
Consonant
Vowel
0

Consonant
Vowel
6_______4_______1
Consonant
Vowel
7
Consonant
Vowel
7
a is Vowel
x is Consonant
0
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
a is Vowel
x is Consonant
5
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
a is True
x is False
0
a is True
x is False
1

Consonant
Vowel
6_______34_______3_______34_______4
a is True
x is False
5
a is True
x is False
6

Consonant
Vowel
4
a is True
x is False
8
a is True
x is False
9

Consonant
Vowel
6_______34_______3
a is True
x is False
4
a is 1
x is 0
3
a is True
x is False
6

 

a is 1
x is 0
5

a is 1
x is 0
6_______47_______7
a is True
x is False
6

a is 1
x is 0
6_______4_______4
a is True
x is False
6

True
False
2

Đầu ra

Consonant
Vowel

Thời gian phức tạp. Ô[1]

Không gian phụ trợ. Ô[1]

Làm thế nào để xử lý chữ in hoa là tốt?

Python3




# Python3 program to check if a given

# character is vowel or consonant.

 

True
False
5

True
False
6

Consonant
Vowel
2
Consonant
Vowel
3

Consonant
Vowel
4_______4_______5
Consonant
Vowel
6
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
9
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
4
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
a is Vowel
x is Consonant
0
Consonant
Vowel
0

Consonant
Vowel
6_______4_______1
Consonant
Vowel
7_______4_______7
a is Vowel
x is Consonant
5
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
a is True
x is False
0
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
00
Consonant
Vowel
0

Consonant
Vowel
6_______4_______1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
06
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
11
Consonant
Vowel
0
Consonant
Vowel
1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
16
Consonant
Vowel
0

Consonant
Vowel
6_______4_______1
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
22
a is True
x is False
1

Consonant
Vowel
6_______34_______3_______34_______4
a is True
x is False
5
a is True
x is False
6

Consonant
Vowel
4
a is True
x is False
8
a is True
x is False
9

Consonant
Vowel
6_______34_______3
a is True
x is False
4
a is 1
x is 0
3
a is True
x is False
6

 

a is 1
x is 0
5

Consonant
Vowel
5
Consonant
Vowel
39
Consonant
Vowel
7
Consonant
Vowel
7
Consonant
Vowel
42
a is True
x is False
9

Consonant
Vowel
4
a is 1
x is 0
6
a is 1
x is 0
7
a is True
x is False
6

Consonant
Vowel
4
a is 1
x is 0
6
Consonant
Vowel
06
a is True
x is False
6

Đầu ra

Consonant
Vowel

Thời gian phức tạp. Ô[1]

Không gian phụ trợ. Ô[1]

sử dụng trường hợp chuyển đổi

Python3




Consonant
Vowel
2
Consonant
Vowel
53

Consonant
Vowel
4_______4_______55
Consonant
Vowel
7
Consonant
Vowel
57

Consonant
Vowel
6_______4_______9
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______4_______4_______34_______9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______24_______0
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______24_______5
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______34_______0
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______4_______00
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______4_______06
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______4_______11
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______4_______16
a is True
x is False
9
a is True
x is False
5
Consonant
Vowel
62

Consonant
Vowel
6_______4_______22
a is True
x is False
9
a is True
x is False
5

Consonant
Vowel
4_______4_______08

Consonant
Vowel
4
Consonant
Vowel
10
Consonant
Vowel
11_______47_______3
a is True
x is False
6

 

Consonant
Vowel
14

a is True
x is False
3
a is True
x is False
4
Consonant
Vowel
17
Consonant
Vowel
18
Consonant
Vowel
19
Consonant
Vowel
9
Consonant
Vowel
21

a is True
x is False
3
a is True
x is False
4
Consonant
Vowel
24
Consonant
Vowel
18
Consonant
Vowel
19
Consonant
Vowel
27
Consonant
Vowel
21

Đầu ra

a is Vowel
x is Consonant

Thời gian phức tạp. Ô[1]

Không gian phụ trợ. Ô[1]

Một cách khác là tìm [] ký tự trong chuỗi chỉ chứa Nguyên âm

Python3




Consonant
Vowel
2
Consonant
Vowel
53

 

Consonant
Vowel
4_______4_______32

Consonant
Vowel
4_______4_______34
Consonant
Vowel
7
Consonant
Vowel
36

Consonant
Vowel
4
Consonant
Vowel
10
a is True
x is False
4_______4_______34
Consonant
Vowel
41
Consonant
Vowel
7
Consonant
Vowel
43
Consonant
Vowel
44
a is True
x is False
6

 

Consonant
Vowel
14

a is True
x is False
3
a is True
x is False
4
Consonant
Vowel
17
Consonant
Vowel
18
Consonant
Vowel
34
Consonant
Vowel
52
Consonant
Vowel
9
Consonant
Vowel
54

a is True
x is False
3
a is True
x is False
4
Consonant
Vowel
24
Consonant
Vowel
18
Consonant
Vowel
34
Consonant
Vowel
52
Consonant
Vowel
27
Consonant
Vowel
54

Đầu ra

a is True
x is False

Thời gian phức tạp. TRÊN]

Không gian phụ trợ. Ô[1]
Cách hiệu quả nhất để kiểm tra Nguyên âm bằng cách sử dụng dịch chuyển bit

Trong ASCII, đây là các giá trị tương ứng của mọi nguyên âm cả ở dạng chữ thường và chữ hoa

Nguyên âmDECHEXBINARY

a

970x6101100001

e

1010x6501100101

i

1050x6901101001

o

1110x6F01101111

u

1170x7501110101

 

A

650x4101000001

E

690x4501000101

I

730x4901001001

O

790x4F01001111

U

850x5501010101

 

Vì các nguyên âm viết thường và viết hoa có cùng 5 LSB. Chúng tôi cần một số 0x208222 mang lại 1 trong LSB của nó sau khi dịch chuyển phải 1, 5, 19, 15 nếu không sẽ cho 0. Các số phụ thuộc vào mã hóa ký tự

 

DECHEXBINARY310x1F0001111121304660x2082221000001000001000100010

 

Python3




Consonant
Vowel
2
Consonant
Vowel
53

 

Consonant
Vowel
4
Consonant
Vowel
10
a is True
x is False
4_______4_______68
Consonant
Vowel
69
Consonant
Vowel
70
Consonant
Vowel
71
Consonant
Vowel
72
Consonant
Vowel
73
Consonant
Vowel
44

Consonant
Vowel
4
Consonant
Vowel
76

 

Consonant
Vowel
14

a is True
x is False
3
a is True
x is False
4
Consonant
Vowel
17
Consonant
Vowel
18
Consonant
Vowel
34
Consonant
Vowel
52
Consonant
Vowel
9
Consonant
Vowel
54

a is True
x is False
3
a is True
x is False
4
Consonant
Vowel
24
Consonant
Vowel
18
Consonant
Vowel
34
Consonant
Vowel
52
Consonant
Vowel
27
Consonant
Vowel
54

Đầu ra

a is 1
x is 0

Thời gian phức tạp. Ô[1]

Không gian phụ trợ. Ô[1]
*Chúng tôi có thể bỏ qua phần [ ch & 0x1f ] trên các máy X86 do SHR/SAR [là >> ] tự động được che dấu thành 0x1f

* Đối với máy, kiểm tra bitmap nhanh hơn kiểm tra bảng, nhưng nếu biến ch được lưu trong thanh ghi thì nó có thể thực hiện nhanh hơn

Sử dụng biểu thức chính quy

Sử dụng biểu thức chính quy để kiểm tra xem một ký tự là nguyên âm hay phụ âm. Biểu thức chính quy là một công cụ mạnh mẽ để so khớp mẫu và có thể được sử dụng để nhanh chóng và dễ dàng kiểm tra xem một ký tự đã cho có khớp với một mẫu cụ thể hay không

Để kiểm tra xem một ký tự có phải là nguyên âm hay không bằng biểu thức chính quy, bạn có thể sử dụng đoạn mã sau

Python3




Consonant
Vowel
94
Consonant
Vowel
95

 

Consonant
Vowel
2
Consonant
Vowel
97

Consonant
Vowel
4
Consonant
Vowel
5
a is Vowel
x is Consonant
00
a is Vowel
x is Consonant
01
a is Vowel
x is Consonant
02

Consonant
Vowel
6_______4_______10
a is Vowel
x is Consonant
05

Consonant
Vowel
4
Consonant
Vowel
10
a is Vowel
x is Consonant
08

 

a is True
x is False
3
a is Vowel
x is Consonant
10
Consonant
Vowel
9
a is Vowel
x is Consonant
12
a is Vowel
x is Consonant
13

a is True
x is False
3
a is Vowel
x is Consonant
10
a is Vowel
x is Consonant
16
a is Vowel
x is Consonant
12
a is Vowel
x is Consonant
18

Đầu ra

True
False

Biểu thức chính quy r'[aeiouAEIOU]' khớp với bất kỳ ký tự nào là nguyên âm viết thường hoặc viết hoa. Ở đó. Hàm match[] tìm kiếm một kết quả khớp ở đầu chuỗi, do đó, nó sẽ trả về True nếu ký tự đã cho là một nguyên âm và False nếu không

Cách tiếp cận này có ưu điểm là ngắn gọn và dễ hiểu, đồng thời có thể dễ dàng sửa đổi để kiểm tra các mẫu khác. Tuy nhiên, nó có thể không hiệu quả như một số cách tiếp cận khác được đề cập trong bài viết, đặc biệt đối với đầu vào lớn

Vui lòng tham khảo bài viết đầy đủ về Chương trình để tìm xem một ký tự là nguyên âm hay Phụ âm để biết thêm chi tiết

Sử dụng phương thức ord[]

Python3




a is Vowel
x is Consonant
19
Consonant
Vowel
7
a is Vowel
x is Consonant
21
a is Vowel
x is Consonant
22
Consonant
Vowel
62
a is Vowel
x is Consonant
24
Consonant
Vowel
62
a is Vowel
x is Consonant
26
Consonant
Vowel
62
a is Vowel
x is Consonant
28
Consonant
Vowel
62
a is Vowel
x is Consonant
30
a is Vowel
x is Consonant
31

a is Vowel
x is Consonant
32
Consonant
Vowel
7
a is Vowel
x is Consonant
21
a is Vowel
x is Consonant
35
Consonant
Vowel
62
a is Vowel
x is Consonant
37
Consonant
Vowel
62
a is Vowel
x is Consonant
39
Consonant
Vowel
62
a is Vowel
x is Consonant
41
Consonant
Vowel
62
a is Vowel
x is Consonant
43
a is Vowel
x is Consonant
31

Consonant
Vowel
2
Consonant
Vowel
53

Consonant
Vowel
4
Consonant
Vowel
5
Consonant
Vowel
70_______24_______50_______24_______51
a is Vowel
x is Consonant
19
Consonant
Vowel
0
Consonant
Vowel
70_______24_______50
a is Vowel
x is Consonant
51
a is Vowel
x is Consonant
57

Làm cách nào để viết chương trình cho nguyên âm và phụ âm trong python?

5 nhận xét về “Chương trình Python kiểm tra ký tự là nguyên âm hay phụ âm” .
nidhisingh151199. s=input[] l=[“a”,”e”,”i”,”o”,”u”,”A”,”E”,”I”,”O”,”U”].
điện tử. letter = input[] nguyên âm = ['a','e','i','o','u'].
Ashish Kumar. Char = đầu vào[].
Ashish Kumar. ký tự = đầu vào [].
sanjeet. #Bạn có thể thử mã này

Làm cách nào để tìm số nguyên âm và phụ âm trong một chuỗi trong python?

Thuật toán. .
Lấy một chuỗi làm đầu vào của người dùng
Khởi tạo biến đếm bằng 0
Lặp lại từng ký tự của chuỗi
Kiểm tra "NẾU" ký tự là một nguyên âm
Nếu ký tự là nguyên âm, hãy tăng giá trị của biến đếm lên 1
Khác tiếp tục lặp qua chuỗi
Phá vỡ vòng lặp khi chuỗi kết thúc

Chủ Đề