首页 > 后端开发 > Python教程 > Python Day-String 使用循环函数逻辑,任务

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

Patricia Arquette
发布: 2024-12-14 18:34:11
原创
106 人浏览过

Python Day-String functions logic using loops,Task

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

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板