首頁 >後端開發 >Python教學 >Day - 字串函數

Day - 字串函數

Barbara Streisand
Barbara Streisand原創
2024-12-16 21:00:261044瀏覽

Day - String functions

1.寫一個程式來檢查給定的金鑰是否可用:

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)
        break
    start+=1
    end+=1
else:
    print('Not Contains')
Contains fruit

2.寫一個程式來找出給定的關鍵位置:

find() 回傳找到它的位置。

txt = "Python is my Favourite Language"
key = 'Python'
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 Python
0 5

3.編寫程式檢查以指定鍵開頭的單字:

startswith() 檢查字串是否以指定值開頭。

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

檢查另一種以指定鍵開頭的單字的方法:

txt = "Apples are good, apple is my favorite fruit"
key = 'Apple'
l = len(key) 
if txt[0:l]==key:
    print('Starts with',key)

Starts with Apple

4.寫程式檢查以指定鍵結尾的單字:

endswith() 檢查字串是否以指定值結尾。

txt = "My Favourite Language is Python"
key = 'Python'
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 Contains')
Ends with Python

檢查單字以指定鍵結尾的另一種方法:

txt = "Apples are good, apple is my favorite fruit"
key = 'fruit'
l = len(key) 
if txt[-l:]==key:
    print('Ends with',key)

Ends with fruit

5.寫一個程式來檢查給定的單字是否為字母:

isalpha() 檢查字串中的所有字元都是字母。

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

6.寫一個程式來檢查給定的單字是否為alnum:

isalnum() 檢查字串中的所有字元都是字母數字。

alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
word = 'pritha017@gmail.com'
for letter in word:
    if letter not in alpha:
        print('Not all are alphabets and numbers')
        break
else:
    print('All are alphabets and numbers')
Not all are alphabets and numbers

7.寫一個程式來檢查給定的單字是否小寫:

islower() 檢查字串中的所有字元均為小寫。

alpha = 'abcdefghijklmnopqrstuvwxyz'
word = 'lakshmipritha'
for letter in word:
    if letter not in alpha:
        print('Not all are lowercase alphabets')
        break
else:
    print('All are lowercase alphabets')
All are lowercase alphabets

8.寫一個程式來檢查給定的單字是否大寫:

isupper() 檢查字串中的所有字元均為大寫。

alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
word = 'LAKSHMIPRITHA'
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

9.寫一個程式檢查空間是否可用:

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 = "HELLO, AND WELCOME TO MY WORLD!"
for letter in txt:
    if letter>='A' and letter<'Z':
        letter = ord(letter)+32
        letter = chr(letter)
    print(letter,end='')

hello, and welcome to my world!

2.寫一個將小寫字母轉換為大寫字母的程式:

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

txt = "hello, and welcome to my world!"
for letter in txt:
    if letter>='a' and letter<'z':
        letter = ord(letter)-32
        letter = chr(letter)
    print(letter,end='')
HELLO, AND WELCOME TO MY WORLD!

3.寫一個程式來檢查給定的字串標題是否:

istitle() 檢查字串是否遵循標題規則。

以上是Day - 字串函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn