Viết chương trình Python để kiểm tra xem một chuỗi đã cho có chứa chữ hoa, chữ thường, số và độ dài tối thiểu không
Giải pháp mẫu. -
Mã Python
def check_string[s]:
messg = []
if not any[x.isupper[] for x in s]:
messg.append['String must have 1 upper case character.']
if not any[x.islower[] for x in s]:
messg.append['String must have 1 lower case character.']
if not any[x.isdigit[] for x in s]:
messg.append['String must have 1 number.']
if len[s] < 8:
messg.append['String length should be atleast 8.']
if not messg:
messg.append['Valid string.']
return messg
s = input["Input the string: "]
print[check_string[s]]
Đầu ra mẫu
Input the string: W3resource ['Valid string.']
Sơ đồ
Trực quan hóa việc thực thi mã Python
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên
353, 5
Hoạt động cuối. Ngày 22 tháng 11 năm 2017, 3. 56 AM EST
Cảm ơn 5 lần trong 5 bài viết
Đếm tất cả các từ bắt đầu bằng chữ in hoa trong chuỗi bằng từ điển python
Xin chào,
Tôi đã viết đoạn mã python sau để lưu trữ các từ bắt đầu bằng chữ in hoa vào từ điển dưới dạng khóa và không có giá trị nào xuất hiện dưới dạng giá trị trong từ điển này so với khóa
#!/usr/bin/env python import sys import re hash = {} # initialize an empty dictinonary for line in sys.stdin.readlines[]: for word in line.strip[].split[]: # removing newline char at the end of the line #l = re.search["\b[A-Z]\S+", word] if word[0].isupper[]: if word in hash: hash[word] += 1 else: hash[word] = 1 for word, cnt in hash.iteritems[]: # iterating over the dictionary items sys.stdout.write["%d %s\n" % [cnt, word]]
Trong đoạn mã trên, tôi đang sử dụng chỉ mục mảng để kiểm tra chữ hoa. Nhưng khi tôi cố mã hóa lại nó bằng biểu thức chính quy như.
______3
Trong phần này, chúng tôi lặp lại từng ký tự trong Chuỗi, kiểm tra chữ hoa bằng isupper[], nếu tìm thấy, Chuỗi được gắn cờ là True
Python3
Input the string: W3resource ['Valid string.']29
Input the string: W3resource ['Valid string.']30
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.0
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.1
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.2
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.4
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.5
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.8
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']41
Input the string: W3resource ['Valid string.']42
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input the string: W3resource ['Valid string.']44
________ 145 ________ 146 ________ 147 ________ 148
Input the string: W3resource ['Valid string.']49
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']49
Input the string: W3resource ['Valid string.']42
Input the string: W3resource ['Valid string.']43
Input the string: W3resource ['Valid string.']44
Input the string: W3resource ['Valid string.']42
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input the string: W3resource ['Valid string.']47
Input the string: W3resource ['Valid string.']44
Input the string: W3resource ['Valid string.']49
Input the string: W3resource ['Valid string.']40
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7____143
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']46
đầu ra
Input the string: W3resource ['Valid string.']4
Phương pháp #2. Sử dụng any[] + isupper[]
Trong phần này, chúng tôi sử dụng any[] để kiểm tra bất kỳ ký tự nào nếu đó là ký tự viết hoa
Python3
Input the string: W3resource ['Valid string.']29
Input the string: W3resource ['Valid string.']30
Input the string: W3resource ['Valid string.']49
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.1
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.2
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.4
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.5
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.8
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']41
Input the string: W3resource ['Valid string.']41
Input the string: W3resource ['Valid string.']42
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input the string: W3resource ['Valid string.']44
Input the string: W3resource ['Valid string.']45
Input the string: W3resource ['Valid string.']45
Input the string: W3resource ['Valid string.']46
Input the string: W3resource ['Valid string.']47
Input the string: W3resource ['Valid string.']49
Input the string: W3resource ['Valid string.']40
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7____143
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']46
đầu ra
Input the string: W3resource ['Valid string.']4
Phương pháp #3. Sử dụng biểu thức chính quy[]
Regex thích hợp có thể được sử dụng để thực hiện nhiệm vụ này. Điều này kiểm tra bất kỳ chữ hoa nào trong Chuỗi
Python3
Input the string: W3resource ['Valid string.']29
Input the string: W3resource ['Valid string.']30
Input the string: W3resource ['Valid string.']49
Input the string: W3resource ['Valid string.']290
Input the string: W3resource ['Valid string.']291
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.1
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.2
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.4
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.5
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.8
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']41
Input the string: W3resource ['Valid string.']303
Input the string: W3resource ['Valid string.']42
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input the string: W3resource ['Valid string.']306____1307
Input the string: W3resource ['Valid string.']308
Input the string: W3resource ['Valid string.']309
Input the string: W3resource ['Valid string.']40
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7____143
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']46
đầu ra
Input the string: W3resource ['Valid string.']4
Phương pháp #4. Sử dụng bất kỳ[] + giá trị ASCII
Kiểm tra xem mỗi ký tự có nằm trong nhóm chữ hoa chữ thường của các giá trị ASCII không
Python3
Input the string: W3resource ['Valid string.']29
Input the string: W3resource ['Valid string.']30
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.09
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.1
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.2
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.4
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.5
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.6
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.7
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.8
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.9
Input the string: W3resource ['Valid string.']40
Input the string: W3resource ['Valid string.']41
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.21
Input the string: W3resource ['Valid string.']42
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input the string: W3resource ['Valid string.']44_______47____426____427
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.29
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.30
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.26
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.32
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.34
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.35
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.36
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.26
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.38
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.3
Input : test_str = 'geeksforgEeks' Output : True Explanation : E is uppercase in String.40
Input the string: W3resource ['Valid string.']45
Input the string: W3resource ['Valid string.']46
Input the string: W3resource ['Valid string.']47
Input the string: W3resource ['Valid string.']49