搜尋

Day - 字串函數

Dec 16, 2024 pm 09:00 PM

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





<pre class="brush:php;toolbar:false">Contains fruit

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

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

txt = "Python is my Favourite Language"
key = 'Python'
l = len(key)
start = 0 
end = l
while end





<pre class="brush:php;toolbar:false">Contains Python
0 5

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

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

txt = "Python is my Favourite Language"
key = 'Python'
l = len(key)
start = 0 
end = l
while end





<pre class="brush:php;toolbar:false">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





<pre class="brush:php;toolbar:false">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





<pre class="brush:php;toolbar:false">hello, and welcome to my world!

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

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

txt = "hello, and welcome to my world!"
for letter in txt:
    if letter>='a' and letter





<pre class="brush:php;toolbar:false">HELLO, AND WELCOME TO MY WORLD!

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

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

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python中的合併列表:選擇正確的方法Python中的合併列表:選擇正確的方法May 14, 2025 am 12:11 AM

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

如何在Python 3中加入兩個列表?如何在Python 3中加入兩個列表?May 14, 2025 am 12:09 AM

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

Python串聯列表字符串Python串聯列表字符串May 14, 2025 am 12:08 AM

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

Python執行,那是什麼?Python執行,那是什麼?May 14, 2025 am 12:06 AM

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python:關鍵功能是什麼Python:關鍵功能是什麼May 14, 2025 am 12:02 AM

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python:編譯器還是解釋器?Python:編譯器還是解釋器?May 13, 2025 am 12:10 AM

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

python用於循環與循環時:何時使用哪個?python用於循環與循環時:何時使用哪個?May 13, 2025 am 12:07 AM

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

Python循環:最常見的錯誤Python循環:最常見的錯誤May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具