首页 >后端开发 >Python教程 >Day - 字符串函数

Day - 字符串函数

Barbara Streisand
Barbara Streisand原创
2024-12-16 21:00:261044浏览

Day - String functions

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn