Day - 字符串函数
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...
