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中文网其它相关文章!
版权声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系 yyfuon@163.com