您的位置 首页 编程知识

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

1) find(): 在字符串中搜索指定值并返回找到它的位置。 txt = "i love many…

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

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中文网其它相关文章!

本文来自网络,不代表四平甲倪网络网站制作专家立场,转载请注明出处:http://www.elephantgpt.cn/4665.html

作者: nijia

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

18844404989

在线咨询: QQ交谈

邮箱: 641522856@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部