Trước đây, chúng tôi phải kiểm tra xem chuỗi bắt đầu bằng một nguyên âm. Trong bài viết này, chúng tôi sẽ kiểm tra xem chuỗi có chứa nguyên âm trong Python không. Các chữ cái a, e, i, o, u và a, e, i, o, u là nguyên âm. Hãy nhớ rằng, tất cả các chữ cái khác là phụ âm. Đã thực hiện chuỗi bằng cách sử dụng input [] trong khi khai báo chuỗi tên biến. Sau đó, kiểm tra xem chuỗi có chứa các nguyên âm bằng cách sử dụng câu lệnh FOR LOOP và IF-OLSE không. Chuỗi chứa các nguyên âm hoặc không in bằng hàm in []. Đầu ra cho các giá trị đầu vào Test-Case-1:- Chuỗi: Biết chương trình, chuỗi chứa nguyên âm. Đầu ra cho các giá trị đầu vào Test-Case-2:- Chuỗi: HMMNO, Chuỗi không chứa nguyên âm. Chương trình này giống như chương trình trên nhưng trong chương trình này, chúng tôi sử dụng các phương thức khác nhau. Trong phương pháp này, chúng tôi sẽ kiểm tra xem một chuỗi có chứa nguyên âm bằng cách hiểu danh sách không. Output:- Chuỗi: Pythonyes, Chuỗi chứa nguyên âm. Trong chương trình trên, chúng tôi sẽ kiểm tra xem một chuỗi có chứa các nguyên âm sử dụng vòng lặp For nhưng trong chương trình này, chúng tôi đang sử dụng vòng lặp trong khi không. Output:- Chuỗi: bcdfghno, chuỗi không chứa nguyên âm.# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
for char in string:
if char in 'aeiouAEIOU':
return True
return False
# take inputs
string = input['String: ']
# function call
if [checkVowels[string] == True]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
Yes, String contains vowels.
No, String does not contain vowels.Python để kiểm tra xem chuỗi có chứa nguyên âm không
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
Yes, String contains vowels.Kiểm tra xem chuỗi có chứa nguyên âm trong python bằng cách sử dụng vòng lặp không
# Python program to check if string contains vowels using while loop
def checkVowels[string]: #use-defined function
count = 0
num_vowels = 0
# to count the vowels
while count < len[string]:
if string[count] == "a" or string[count] == "e"
or string[count] == "i" or string[count] == "o"
or string[count] == "u" or string[count] == "A"
or string[count] == "E" or string[count] == "I"
or string[count] == "O" or string[count] == "U":
num_vowels = num_vowels+1
count = count+1
return num_vowels
# take inputs
string = input['String: ']
# calling function
if [checkVowels[string] != 0]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
No, String does not contain vowels.
Kiểm tra xem một chuỗi có chứa nguyên âm trong python # không
Để kiểm tra xem một chuỗi có chứa nguyên âm không:
- Sử dụng biểu thức máy phát để lặp qua chuỗi.
- Kiểm tra xem mỗi ký tự là một nguyên âm.
- Nếu điều kiện được đáp ứng cho bất kỳ ký tự nào, chuỗi chứa các nguyên âm.
Copied!
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} my_str = 'bobby' if any[char in vowels for char in my_str]: # 👇️ this runs print['The string contains at least one vowel'] else: print['The string does NOT contain any vowels'] print[any[char in vowels for char in 'hadz']] # 👉️ True print[any[char in vowels for char in 'hdz']] # 👉️ False print[any[char in vowels for char in '']] # 👉️ False
Chúng tôi đã sử dụng một biểu thức máy phát để lặp qua chuỗi.
Biểu thức của máy phát được sử dụng để thực hiện một số hoạt động cho mọi phần tử hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.
Trên mỗi lần lặp, chúng tôi sử dụng toán tử in
để kiểm tra xem ký tự hiện tại có chứa trong nguyên âm
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
0 không.Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
1 đánh giá thành # Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
2 nếu # Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
3 là thành viên của # Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
4, nếu không nó sẽ đánh giá thành # Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
5.Bất kỳ chức năng nào lấy một sự khác biệt như một đối số và trả về
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
2 nếu bất kỳ yếu tố nào trong điều đó là sự thật.Copied!
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} print[any[char in vowels for char in 'hadz']] # 👉️ True print[any[char in vowels for char in 'hdz']] # 👉️ False print[any[char in vowels for char in '']] # 👉️ False
Nếu điều kiện được đáp ứng cho bất kỳ ký tự nào trong chuỗi, hàm
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
7 trả về # Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
2 và các mạch ngắn.Ngoài ra, bạn có thể sử dụng vòng lặp
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
9.Kiểm tra xem một chuỗi có chứa nguyên âm bằng cách sử dụng vòng lặp #
Để kiểm tra xem một chuỗi có chứa nguyên âm không:
- Sử dụng biểu thức máy phát để lặp qua chuỗi.
- Kiểm tra xem mỗi ký tự là một nguyên âm.
- Nếu điều kiện được đáp ứng, chuỗi chứa các nguyên âm.
Copied!
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} def contains_vowels[string]: for char in string: if char in vowels: return True return False print[contains_vowels['bobby']] # 👉️ True print[contains_vowels['hadz']] # 👉️ True print[contains_vowels['hdz']] # 👉️ False print[contains_vowels['']] # 👉️ False
Chúng tôi đã sử dụng vòng lặp
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
9 để lặp qua chuỗi.Trên mỗi lần lặp, chúng tôi kiểm tra xem ký tự hiện tại có phải là nguyên âm không.
Nếu điều kiện được đáp ứng, chúng tôi sẽ trả lại
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
2 để thoát khỏi chức năng.Nếu điều kiện không bao giờ được đáp ứng, hàm trả về
# Python program to check if string contains vowels
def checkVowels[string]: #use-defined function
# check the string contains vowels
vowels = [each for each in string if each in "aeiouAEIOU"]
return vowels
# take inputs
string = input['String: ']
# function call
if [checkVowels[string]]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
5.Bạn có thể sử dụng cùng một cách tiếp cận để có được các nguyên âm được chứa trong chuỗi [nếu có].
Copied!
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} my_str = 'abcde' vowels_in_string = [] for char in my_str: if char in vowels: print[f'{char} is a vowel'] if char not in vowels_in_string: vowels_in_string.append[char] else: print[f'{char} is a consonant'] # a is a vowel # b is a consonant # c is a consonant # d is a consonant # e is a vowel print[vowels_in_string] # 👉️ ['a', 'e']
Chúng tôi đã sử dụng toán tử
# Python program to check if string contains vowels using while loop
def checkVowels[string]: #use-defined function
count = 0
num_vowels = 0
# to count the vowels
while count < len[string]:
if string[count] == "a" or string[count] == "e"
or string[count] == "i" or string[count] == "o"
or string[count] == "u" or string[count] == "A"
or string[count] == "E" or string[count] == "I"
or string[count] == "O" or string[count] == "U":
num_vowels = num_vowels+1
count = count+1
return num_vowels
# take inputs
string = input['String: ']
# calling function
if [checkVowels[string] != 0]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
4 để đảm bảo rằng chúng tôi không thêm các nguyên âm trùng lặp vào danh sách # Python program to check if string contains vowels using while loop
def checkVowels[string]: #use-defined function
count = 0
num_vowels = 0
# to count the vowels
while count < len[string]:
if string[count] == "a" or string[count] == "e"
or string[count] == "i" or string[count] == "o"
or string[count] == "u" or string[count] == "A"
or string[count] == "E" or string[count] == "I"
or string[count] == "O" or string[count] == "U":
num_vowels = num_vowels+1
count = count+1
return num_vowels
# take inputs
string = input['String: ']
# calling function
if [checkVowels[string] != 0]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
5.
6 trả về sự phủ định của # Python program to check if string contains vowels using while loop
def checkVowels[string]: #use-defined function
count = 0
num_vowels = 0
# to count the vowels
while count < len[string]:
if string[count] == "a" or string[count] == "e"
or string[count] == "i" or string[count] == "o"
or string[count] == "u" or string[count] == "A"
or string[count] == "E" or string[count] == "I"
or string[count] == "O" or string[count] == "U":
num_vowels = num_vowels+1
count = count+1
return num_vowels
# take inputs
string = input['String: ']
# calling function
if [checkVowels[string] != 0]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
7.# Python program to check if string contains vowels using while loop
def checkVowels[string]: #use-defined function
count = 0
num_vowels = 0
# to count the vowels
while count < len[string]:
if string[count] == "a" or string[count] == "e"
or string[count] == "i" or string[count] == "o"
or string[count] == "u" or string[count] == "A"
or string[count] == "E" or string[count] == "I"
or string[count] == "O" or string[count] == "U":
num_vowels = num_vowels+1
count = count+1
return num_vowels
# take inputs
string = input['String: ']
# calling function
if [checkVowels[string] != 0]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
Phương thức Danh sách.Append [] thêm một mục vào cuối danh sách.
Copied!
my_list = ['bobby', 'hadz'] my_list.append['com'] print[my_list] # 👉️ ['bobby', 'hadz', 'com']
Nếu bạn cần đếm các nguyên âm trong chuỗi, hãy tạo một từ điển với các nguyên âm làm khóa.
Copied!
vowels = 'aeiou' my_str = 'bobbyhadz.com' vowels_count = {vowel: my_str.lower[].count[vowel] for vowel in vowels} print[vowels_count] # 👉️ {'a': 1, 'e': 0, 'i': 0, 'o': 2, 'u': 0} print[vowels_count['a']] # 👉️ 1 print[vowels_count['o']] # 👉️ 2
Chúng tôi đã sử dụng một sự hiểu biết chính thống để lặp lại trên một chuỗi chứa các nguyên âm.
Dictorsions rất giống với sự hiểu biết trong danh sách.
Họ thực hiện một số hoạt động cho mọi cặp giá trị khóa trong từ điển hoặc chọn một tập hợp con của các cặp giá trị khóa đáp ứng một điều kiện.
Phương thức str.count trả về số lần xuất hiện của một chuỗi con trong một chuỗi.
Từ điển
# Python program to check if string contains vowels using while loop
def checkVowels[string]: #use-defined function
count = 0
num_vowels = 0
# to count the vowels
while count < len[string]:
if string[count] == "a" or string[count] == "e"
or string[count] == "i" or string[count] == "o"
or string[count] == "u" or string[count] == "A"
or string[count] == "E" or string[count] == "I"
or string[count] == "O" or string[count] == "U":
num_vowels = num_vowels+1
count = count+1
return num_vowels
# take inputs
string = input['String: ']
# calling function
if [checkVowels[string] != 0]:
print['Yes, String contains vowels.']
else:
print['No, String does not contain vowels.']
8 chứa số lần xuất hiện của mỗi nguyên âm trong chuỗi.