2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

python入門(mén)到脫坑經(jīng)典案例—判斷字符串的 元素組成

 江海博覽 2025-06-06

下面詳細(xì)講解如何判斷字符串的元素組成。這個(gè)案例將幫助你掌握字符串處理的基本技巧,并了解多種驗(yàn)證方法。我們從基礎(chǔ)到進(jìn)階逐步深入:


1. 基礎(chǔ)判斷方法

(1)檢查字符串是否只包含字母將詳細(xì)講解如何判斷字符串的元素組成。這個(gè)案例將幫助你掌握字符串處理的基本技巧,并了解多種驗(yàn)證方法。我們從基礎(chǔ)到進(jìn)階逐步深入:


1. 基礎(chǔ)判斷方法

(1)檢查字符串是否只包含字母

text = 'Hello' print(text.isalpha()) # 輸出 True(純字母) text2 = 'Hello123' print(text2.isalpha()) # 輸出 False

(2)檢查是否只包含數(shù)字

num = '12345'
print(num.isdigit())   # 輸出 True(純數(shù)字)

num2 = '123abc'
print(num2.isdigit())  # 輸出 False

(3)檢查是否字母或數(shù)字組合

mixed = 'Hello123' print(mixed.isalnum()) # 輸出 True(字母或數(shù)字) mixed2 = 'Hello!' print(mixed2.isalnum()) # 輸出 False(含特殊字符)

2. 自定義條件判斷

(1)檢查是否包含特定字符

def contains_char(text, char):
    return char in text  # 使用in關(guān)鍵字

print(contains_char('apple', 'p'))  # 輸出 True
print(contains_char('apple', 'z'))  # 輸出 False

(2)統(tǒng)計(jì)各類(lèi)字符數(shù)量

text = 'PyThon3.8!' lower = sum(1 for c in text if c.islower()) # 小寫(xiě)字母 upper = sum(1 for c in text if c.isupper()) # 大寫(xiě)字母 digit = sum(1 for c in text if c.isdigit()) # 數(shù)字 other = len(text) - lower - upper - digit # 其他字符 print(f'小寫(xiě):{lower},大寫(xiě):{upper},數(shù)字:{digit},其他:{other}')

3. 正則表達(dá)式進(jìn)階版

import re

def check_composition(text):
    if re.fullmatch(r'[A-Za-z]+', text):    # 僅字母
        return '純字母'
    elif re.fullmatch(r'\d+', text):         # 僅數(shù)字
        return '純數(shù)字'
    elif re.fullmatch(r'[A-Za-z\d]+', text): # 字母+數(shù)字
        return '字母數(shù)字組合'
    else:
        return '包含特殊字符'

print(check_composition('Hello'))  # 輸出 '純字母'
print(check_composition('123!@#')) # 輸出 '包含特殊字符'

正則表達(dá)式說(shuō)明:

  • [A-Za-z]:匹配所有字母
  • \d:匹配數(shù)字(等價(jià)于[0-9])
  • +:匹配前一個(gè)字符1次或多次
  • fullmatch:要求整個(gè)字符串匹配模式

4. 密碼強(qiáng)度檢測(cè)案例

def password_strength(pwd): if len(pwd) < 8: return '弱密碼:至少8位' has_upper = any(c.isupper() for c in pwd) has_lower = any(c.islower() for c in pwd) has_digit = any(c.isdigit() for c in pwd) has_special = not pwd.isalnum() strength = 0 if has_upper: strength += 1 if has_lower: strength += 1 if has_digit: strength += 1 if has_special: strength += 1 return ['弱', '中', '強(qiáng)', '極強(qiáng)'][strength-1] print(password_strength('Abc123!')) # 輸出 '極強(qiáng)'

5. 字符串組成可視化

from collections import Counter

text = 'Python3.8 發(fā)布!2020年'
counter = Counter(text)

print('字符頻率統(tǒng)計(jì):')
for char, count in counter.items():
    print(f''{char}': {count}次')

# 輸出示例:
# 'P': 1次
# 'y': 1次
# '!': 1次
# '2': 2次 等

6. 綜合練習(xí)

嘗試實(shí)現(xiàn)以下功能:

  1. 檢查字符串是否是回文(正反讀相同)
  2. 統(tǒng)計(jì)字符串中每個(gè)元音字母的出現(xiàn)次數(shù)
  3. 驗(yàn)證電子郵件格式是否合法(包含@和.)

示例代碼框架:

# 回文檢測(cè) def is_palindrome(text): clean = ''.join(c.lower() for c in text if c.isalnum()) return clean == clean[::-1] print(is_palindrome('A man, a plan, a canal: Panama')) # 輸出 True

7. 常見(jiàn)問(wèn)題解答

Q:如何判斷字符串是否全為中文?

def is_chinese(text):
    return all('\u4e00' <= char <= '\u9fff' for char in text)

Q:如何檢查字符串是否包含空格?

print(' ' in 'hello world') # 方法1 print('hello world'.isspace()) # 方法2(檢查是否全為空格)

Q:大小寫(xiě)敏感如何控制?

text = 'Hello'
print(text.lower() == 'hello')  # 轉(zhuǎn)換為小寫(xiě)比較

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多