Cải thiện bài viết
Lưu bài viết
Phương thức Python StringAsalpha [] được sử dụng để kiểm tra xem tất cả các ký tự trong chuỗi là bảng chữ cái. isalpha[] method is used to check whether all characters in the String is an alphabet.
Chuỗi python isalpha [] Phương thức cú pháp:
Cú pháp: & nbsp; String.isalpha [] string.isalpha[]
Tham số: isalpha [] không lấy bất kỳ tham số nàoisalpha[] does not take any parameters
Returns:
- Đúng: nếu tất cả các ký tự trong chuỗi là bảng chữ cái.: If all characters in the string are alphabet.
- Sai: Nếu chuỗi chứa 1 hoặc nhiều không phải alphabet.: If the string contains 1 or more non-alphabets.
Lỗi và ngoại lệ:
- Nó không chứa đối số, do đó xảy ra lỗi nếu tham số được truyền
- Cả bảng chữ cái viết hoa và chữ thường trở lại
- Không gian không được coi là bảng chữ cái, do đó nó trả về “sai”
Chuỗi python isalpha [] Phương thức Ví dụ:
Python3
string
=
"geeks"
print
[string.isalpha[]]
Output:
True
Ví dụ 1: Làm việc của Isalpha []
Python3
string
=
True False False2
print
[string.isalpha[]]
string
=
True False False7
print
[string.isalpha[]]
string
=
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush2
print
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush4
Output:
True False False
Ví dụ 2: Ứng dụng thực tế
Đưa ra một chuỗi trong Python, hãy đếm số lượng bảng chữ cái trong chuỗi và in bảng chữ cái.
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush
Algorithm:
- Khởi tạo một chuỗi mới và bộ đếm biến thành 0. & nbsp;
- Đi qua ký tự chuỗi đã cho bằng ký tự lên đến chiều dài của nó, kiểm tra xem ký tự là bảng chữ cái. & NBSP;
- Nếu đó là bảng chữ cái, hãy tăng bộ đếm 1 và thêm nó vào một chuỗi mới, khác đi qua ký tự tiếp theo. & NBSP;
- In giá trị của bộ đếm và chuỗi mới.
Python3
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush5
=
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush2
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush8
=
11 AyushSaxena 5 Ayush0
11 AyushSaxena 5 Ayush1
=
11 AyushSaxena 5 Ayush3
11 AyushSaxena 5 Ayush4
=
11 AyushSaxena 5 Ayush3
11 AyushSaxena 5 Ayush7
11 AyushSaxena 5 Ayush8
11 AyushSaxena 5 Ayush9
def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
0def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
1def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
2 def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
3=
=
def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
6def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
7def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
8Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush8
string
0=
string
2def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
8string
4string
0=
string
7print
string
9
print
=
1
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush5
=
True False False7
Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush8
=
11 AyushSaxena 5 Ayush0
11 AyushSaxena 5 Ayush7
11 AyushSaxena 5 Ayush8
11 AyushSaxena 5 Ayush9
def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
0def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
1def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
2 def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
3=
=
def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
6def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
7def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
8Input : string = 'Ayush Saxena' Output : 11 AyushSaxena Input : string = 'Ayush0212' Output : 5 Ayush8
string
0=
string
2def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
8print
5string
0=
string
7print
string
9
print
[string.isalpha[]]
2
Output:
11 AyushSaxena 5 Ayush
8def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]
Input : string = 'Ayush Saxena'
Output : 11
AyushSaxena
Input : string = 'Ayush0212'
Output : 5
Ayush
8string
0=
string
2
O[n]
Độ phức tạp về thời gian: O [n]O[n]
Không gian phụ trợ: O [n]
def check[count]:
lowercase = 0
uppercase = 0
other = 0
low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for n in count:
if n in low:
lowercase += 1
elif n in upper:
uppercase += 1
else:
other += 1
print["There are " + str[lowercase] + " lowercase letters."]
print["There are " + str[uppercase] + " uppercase letters."]
print["There are " + str[other] + " other elements to this sentence."]