首頁 > 後端開發 > Python教學 > Python Day-String 使用循環函數邏輯,任務

Python Day-String 使用循環函數邏輯,任務

Patricia Arquette
發布: 2024-12-14 18:34:11
原創
106 人瀏覽過

Python Day-String functions logic using loops,Task

1) find(): 在字串中搜尋指定值並傳回找到它的位置。

txt = "I love many fruits, apple is my favorite fruit"
key = 'fruit'
l = len(key)
start = 0 
end = l
while end<=len(txt):
    if txt[start:end] == key:
        print('Contains', key)
        print(start, end-1)
        break
    start+=1
    end+=1
else:
    print('Not Contains')
登入後複製

輸出:

Contains fruit
12 16
登入後複製

2)startswith():如果字串以指定值開頭,則傳回 true

範例:1

#starts with: 
txt = "Python is my favourite language"
key = 'Python'
l = len(key)
start = 0 
end = l
while end<len(txt):
    if txt[start:end] == key:
        if start == 0:
            print("Starts with",key)
        break
    start+=1
    end+=1
else:
    print('Not Contains')
登入後複製

輸出:

Starts with Python

登入後複製

範例:2

txt = "Apples are good, apple is my favorite fruit"
key = 'Apple'
#starts with
l = len(key) #5
if txt[0:l] == key:
    print('Starts with',key)
登入後複製

輸出:

Starts with Apple
登入後複製

3)endswith():如果字串以指定值結尾,則傳回 true。
例:1

txt = "Apples are good, apple is my favorite fruit"
key = 'fruit'
#starts with
l = len(key) #5
if txt[-len(key):] == key:
    print('Ends with',key)
登入後複製

輸出:

Ends with fruit
登入後複製

範例:2

txt = "Python is my favourite language"
key = 'language'
l = len(key)
start = 0 
end = l
while end<=len(txt):
    if txt[start:end] == key:
        if end==len(txt):
            print("Ends with",key)
        break
    start+=1
    end+=1
else:
    print('Not ending with language')
登入後複製

輸出:

Ends with language
登入後複製

4) isalpha(): 如果字串中的所有字元都在字母表中,則傳回 True。

方法:1

word = 'abcdEFGH'
for letter in word:
    if letter>='a' and letter<='z' or letter>='A' and letter<='Z':
        continue
    else:
        print('not all are alphabets')
        break
else:
    print('All are alphabets')
登入後複製

方法:2

alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
word = 'abcdEFGH'
for letter in word:
    if letter not in alpha:
        print('Not all are alphabets')
        break
else:
    print('All are alphabets')
登入後複製

輸出:

All are alphabets
登入後複製

5) isalnum(): 如果字串中的所有字元都是字母數字,則傳回 True。

#isalnum
alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
word = 'abcd1234'
for letter in word:
    if letter not in alpha:
        print('Not all are alphabets and numbers')
        break
else:
    print('All are alphabets and numbers')
登入後複製

輸出:

All are alphabets and numbers
登入後複製

6) islower(): 如果字串中的所有字元都是小寫,則傳回 True。

#islower
alpha = 'abcdefghijklmnopqrstuvwxyz'
word = 'lakshmipritha'
for letter in word:
    if letter not in alpha:
        print('Not all are lower alphabets')
        break
else:
    print('All are lower alphabets')
登入後複製

輸出:

All are lower alphabets
登入後複製

7) isupper(): 如果字串中的所有字元均為大寫,則傳回 True。

#isupper
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
word = 'GURU'
for letter in word:
    if letter not in alpha:
        print('Not all are UPPERCASE alphabets')
        break
else:
    print('All are UPPERCASE alphabets')
登入後複製

輸出:

All are UPPERCASE alphabets
登入後複製

8) isspace(): 如果字串中的所有字元都是空格,則傳回 True。

#isspace

word = '        '
for letter in word:
    if letter != ' ':
        print("not all are spaces")
        break
else:
    print('All are spaces')
登入後複製

輸出:

All are spaces
登入後複製

任務:
1) lower(): 將字串轉換為小寫。

txt = "PYTHON IS MY FAVOURITE LANGUAGE"
for letter in txt:
    if letter>='A' and letter<='Z':
        letter = ord(letter)+32
        letter = chr(letter)
    print(letter,end='')
登入後複製

輸出:

python is my favourite language
登入後複製

2) upper(): 將字串轉換為大寫。

txt = "python is my favourite language"
for letter in txt:
    if letter>='a' and letter<='z':
        letter = ord(letter)-32
        letter = chr(letter)
    print(letter,end='')
登入後複製

輸出:

PYTHON IS MY FAVOURITE LANGUAGE
登入後複製

以上是Python Day-String 使用循環函數邏輯,任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板