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

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

Patricia Arquette
Patricia Arquette原创
2024-12-14 18:34:11107浏览

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

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