PHP前端开发

Python Day-String 使用循环函数逻辑,任务

百变鹏仔 5天前 #Python
文章标签 函数

1) find(): 在字符串中搜索指定值并返回找到它的位置。

txt = "i love many fruits, apple is my favorite fruit"key = 'fruit'l = len(key)start = 0 end = lwhile end<=len(txt):    if txt[start:end] == key:        print('contains', key)        print(start, end-1)        break    start+=1    end+=1else:    print('not contains')

输出:

contains fruit12 16

2)startswith():如果字符串以指定值开头,则返回 true

示例:1

#starts with: txt = "python is my favourite language"key = 'python'l = len(key)start = 0 end = lwhile end<len(txt):    if txt[start:end] == key:        if start == 0:            print("starts with",key)        break    start+=1    end+=1else:    print('not contains')

输出:

starts with python

示例:2

txt = "apples are good, apple is my favorite fruit"key = 'apple'#starts withl = len(key) #5if 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 withl = len(key) #5if 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 = lwhile end<=len(txt):    if txt[start:end] == key:        if end==len(txt):            print("ends with",key)        break    start+=1    end+=1else:    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')        breakelse:    print('all are alphabets')

方法:2

alpha = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'word = 'abcdefgh'for letter in word:    if letter not in alpha:        print('not all are alphabets')        breakelse:    print('all are alphabets')

输出:

all are alphabets

5) isalnum(): 如果字符串中的所有字符都是字母数字,则返回 true。

#isalnumalpha = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'word = 'abcd1234'for letter in word:    if letter not in alpha:        print('not all are alphabets and numbers')        breakelse:    print('all are alphabets and numbers')

输出:

all are alphabets and numbers

6) islower(): 如果字符串中的所有字符均为小写,则返回 true。

#isloweralpha = 'abcdefghijklmnopqrstuvwxyz'word = 'lakshmipritha'for letter in word:    if letter not in alpha:        print('not all are lower alphabets')        breakelse:    print('all are lower alphabets')

输出:

all are lower alphabets

7) isupper(): 如果字符串中的所有字符均为大写,则返回 true。

#isupperalpha = 'abcdefghijklmnopqrstuvwxyz'word = 'guru'for letter in word:    if letter not in alpha:        print('not all are uppercase alphabets')        breakelse:    print('all are uppercase alphabets')

输出:

all are uppercase alphabets

8) isspace(): 如果字符串中的所有字符都是空格,则返回 true。

#isspaceword = '        'for letter in word:    if letter != ' ':        print("not all are spaces")        breakelse:    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