Home > Backend Development > Python Tutorial > Python Day-String functions logic using loops,Task

Python Day-String functions logic using loops,Task

Patricia Arquette
Release: 2024-12-14 18:34:11
Original
106 people have browsed it

Python Day-String functions logic using loops,Task

1) find(): Searches the string for a specified value and returns the position of where it was found.

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')
Copy after login

Output:

Contains fruit
12 16
Copy after login

2) startswith(): Returns true if the string starts with the specified value

Example: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')
Copy after login

Output:

Starts with Python

Copy after login

Example: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)
Copy after login

Output:

Starts with Apple
Copy after login

3) endswith(): Returns true if the string ends with the specified value.
Example: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)
Copy after login

Output:

Ends with fruit
Copy after login

Example: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')
Copy after login

Output:

Ends with language
Copy after login

4) isalpha(): Returns True if all characters in the string are in the alphabet.

Method: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')
Copy after login

Method: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')
Copy after login

Output:

All are alphabets
Copy after login

5) isalnum(): Returns True if all characters in the string are alphanumeric.

#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')
Copy after login

Output:

All are alphabets and numbers
Copy after login

6) islower(): Returns True if all characters in the string are lower case.

#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')
Copy after login

Output:

All are lower alphabets
Copy after login

7) isupper(): Returns True if all characters in the string are upper case.

#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')
Copy after login

Output:

All are UPPERCASE alphabets
Copy after login

8) isspace(): Returns True if all characters in the string are whitespaces.

#isspace

word = '        '
for letter in word:
    if letter != ' ':
        print("not all are spaces")
        break
else:
    print('All are spaces')
Copy after login

Output:

All are spaces
Copy after login

Tasks:
1) lower(): Converts a string into lower case.

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='')
Copy after login

Output:

python is my favourite language
Copy after login

2) upper(): Converts a string into upper case.

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='')
Copy after login

Output:

PYTHON IS MY FAVOURITE LANGUAGE
Copy after login

The above is the detailed content of Python Day-String functions logic using loops,Task. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template