登录
首页 >  文章 >  python教程

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

来源:dev.to

时间:2024-12-25 15:21:50 368浏览 收藏

目前golang学习网上已经有很多关于文章的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《Python Day-String 使用循环函数逻辑,任务》,也希望能帮助到大家,如果阅读完后真的对你学习文章有帮助,欢迎动动手指,评论留言并分享~

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 使用循环函数逻辑,任务》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>