1) find(): Mencari rentetan untuk nilai yang ditentukan dan mengembalikan kedudukan di mana ia ditemui.
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')
Output:
Contains fruit 12 16
2) bermula dengan(): Mengembalikan benar jika rentetan bermula dengan nilai yang ditentukan
Contoh: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')
Output:
Starts with Python
Contoh: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)
Output:
Starts with Apple
3) berakhir dengan(): Mengembalikan benar jika rentetan berakhir dengan nilai yang ditentukan.
Contoh: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)
Output:
Ends with fruit
Contoh: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')
Output:
Ends with language
4) isalpha(): Mengembalikan Benar jika semua aksara dalam rentetan berada dalam abjad.
Kaedah: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')
Kaedah: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')
Output:
All are alphabets
5) isalnum(): Mengembalikan Benar jika semua aksara dalam rentetan adalah abjad angka.
#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')
Output:
All are alphabets and numbers
6) islower(): Mengembalikan Benar jika semua aksara dalam rentetan adalah huruf kecil.
#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')
Output:
All are lower alphabets
7) isupper(): Mengembalikan Benar jika semua aksara dalam rentetan adalah huruf besar.
#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')
Output:
All are UPPERCASE alphabets
8) isspace(): Mengembalikan Benar jika semua aksara dalam rentetan adalah ruang putih.
#isspace word = ' ' for letter in word: if letter != ' ': print("not all are spaces") break else: print('All are spaces')
Output:
All are spaces
Tugas:
1) lower(): Menukar rentetan kepada huruf kecil.
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='')
Output:
python is my favourite language
2) upper(): Menukar rentetan kepada huruf besar.
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='')
Output:
PYTHON IS MY FAVOURITE LANGUAGE
Atas ialah kandungan terperinci Python Day-String berfungsi logik menggunakan gelung, Tugas. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!