Điều này phù hợp với tôi và cũng tính các phụ âm (hãy nghĩ về nó như một phần thưởng), tuy nhiên, nếu bạn thực sự không muốn tính phụ âm tất cả những gì bạn phải làm là xóa vòng lặp cuối cùng và biến cuối cùng ở phía trên.
Cô ấy là mã Python:
data = input('Please give me a string: ') data = data.lower() vowels = ['a','e','i','o','u'] consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'] vowelCount = 0 consonantCount = 0 for string in data: for i in vowels: if string == i: vowelCount += 1 for i in consonants: if string == i: consonantCount += 1 print('Your string contains %s vowels and %s consonants.' %(vowelCount, consonantCount))Có một chức năng nguyên âm trong Python? Người dùng có thể sử dụng các chức năng tích hợp để kiểm tra xem bảng chữ cái có chức năng nguyên âm trong Python hay không. Bước 2: Sử dụng các hàm Python tích hợp như (thấp hơn (), trên ()), xác định xem đầu vào là nguyên âm hay phụ âm. Bước 3: Nếu ký tự là nguyên âm, nó nên được in. Đây là một chương trình Python để đếm số lượng nguyên âm trong một chuỗi. Mô tả vấn đề Chương trình lấy một chuỗi và đếm số lượng nguyên âm trong một chuỗi. Chương trình/mã nguồn Dưới đây là mã nguồn của chương trình Python để xóa ký tự chỉ mục thứ n khỏi chuỗi không trống. Đầu ra chương trình cũng được hiển thị dưới đây. string=raw_input("Enter string:")
vowels=0
for i in string:
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
print("Number of vowels are:")
print(vowels) Giải thích chương trình 1. Người dùng phải nhập một chuỗi và lưu trữ nó trong một biến. 2. Biến số được khởi tạo về 0. 3. Vòng lặp For được sử dụng để đi qua các ký tự trong chuỗi. 4. Một câu lệnh IF kiểm tra xem ký tự có phải là nguyên âm hay không. 5. Số lượng được tăng lên mỗi khi một nguyên âm gặp phải. 6. Tổng số nguyên âm trong chuỗi được in. Trường hợp kiểm tra thời gian chạy
2. Initialize a count variable to 0.
3. Use a for loop to traverse through the characters in the
string.
4. Use an if statement to check if the character is a vowel or not and increment the count variable if it is a vowel.
5. Print the total number of vowels in the string.
6. Exit.
2. The count
variable is initialized to zero.
3. The for loop is used to traverse through the characters in the string.
4. An if statement checks if the character is a vowel or not.
5. The count is incremented each time a vowel is encountered.
6. The total count of vowels in the string is printed.
Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 3
Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Chương trình Python.
Để thực hành tất cả các chương trình Python, đây là bộ hoàn thành hơn 150 vấn đề và giải pháp Python.
Bước tiếp theo:
- Nhận Giấy chứng nhận miễn phí trong chương trình Python
- Tham gia cuộc thi chứng nhận lập trình Python
- Trở thành một người xếp hạng hàng đầu trong chương trình Python
- Thực hiện các bài kiểm tra lập trình Python
- Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
- Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnSets in Python
Examples:
Input : GeeksforGeeks Output : No. of vowels : 5 Input : Hello World Output : No. of vowels : 3Cho một chuỗi, hãy đếm số nguyên âm có trong chuỗi đã cho bằng cách sử dụng các bộ.{IDE} first, before moving on to the solution.
Điều kiện tiên quyết: Bộ trong Python
1. Create a set of vowels using set() and initialize a count variable to 0.
2. Traverse through the alphabets in the string and
check if the letter in the string is present in set vowel.
3. If it is present, the vowel count is incremented.
Được đề xuất: 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.
Cách tiếp cận: 1. Tạo một tập hợp các nguyên âm bằng SET () và khởi tạo biến đếm thành 0,2. Traverse qua bảng chữ cái trong chuỗi và kiểm tra xem chữ cái trong chuỗi có mặt trong nguyên âm tập hợp không.3. Nếu nó có mặt, số nguyên âm được tăng lên.
Dưới đây là việc thực hiện phương pháp trên:
def vowel_count(______7
string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)0____11 string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)2
string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)4string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)1 string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)6string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)7string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)8string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)9
Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 31 Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 322____23 str Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 35
Input : GeeksforGeeks Output : No. of vowels : 5 Input : Hello World Output : No. of vowels : 3 8string=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)7No. of vowels : 5 0No. of vowels : 5 1
Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 36 Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 37 Case 1: Enter string:Hello world Number of vowels are: 3 Case 2: Enter string:WELCOME Number of vowels are: 322____23 Input : GeeksforGeeks Output : No. of vowels : 5 Input : Hello World Output : No. of vowels : 3 0
vowel_count(strstring=raw_input("Enter string:") vowels=0 for i in string: 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 print("Number of vowels are:") print(vowels)9
Output:
No. of vowels : 5