Home > Backend Development > Python Tutorial > Day - String functions

Day - String functions

Barbara Streisand
Release: 2024-12-16 21:00:26
Original
989 people have browsed it

Day - String functions

1.Write a program to check given key is available or not:

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

2.Write a program to find given key position:

find() returns the position of where it was found.

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')

Copy after login
Contains Python
0 5

Copy after login

3.Write a program to check the word starts with the specified key:

startswith() check if the string starts with the specified value.

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
Starts with Python
Copy after login

Another way to check the word starts with the specified key:

txt = "Apples are good, apple is my favorite fruit"
key = 'Apple'
l = len(key) 
if txt[0:l]==key:
    print('Starts with',key)

Copy after login
Starts with Apple
Copy after login

4.Write a program to check the word ends with the specified key:

endswith() check if the string ends with the specified value.

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

Another way to check the word ends with the specified key:

txt = "Apples are good, apple is my favorite fruit"
key = 'fruit'
l = len(key) 
if txt[-l:]==key:
    print('Ends with',key)

Copy after login
Ends with fruit
Copy after login

5.Write a program to check the given word is alpha or not:

isalpha() check all characters in the string are alphabet.

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
All are alphabets
Copy after login

6.Write a program to check the given word is alnum or not:

isalnum() checks all characters in the string are alphanumeric.

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')
Copy after login
Not all are alphabets and numbers
Copy after login

7.Write a program to check the given word is lowercase or not:

islower() checks all characters in the string are lower case.

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')
Copy after login
All are lowercase alphabets
Copy after login

8.Write a program to check the given word is uppercase or not:

isupper() checks all characters in the string are upper case.

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')
Copy after login
All are uppercase alphabets
Copy after login

9.Write a program to check space is available or not:

isspace() checks spaces in the text.

word = '        '
for letter in word:
    if letter != ' ':
        print("Not all are spaces")
        break
else:
    print('All are spaces')

Copy after login
All are spaces
Copy after login

Task:

1.Write a program to convert Uppercase into lowercase:

lower() converts a string into lower case.

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='')

Copy after login
hello, and welcome to my world!
Copy after login

2.Write a program to convert lowercase into uppercase:

upper() converts a string into upper case.

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='')
Copy after login
HELLO, AND WELCOME TO MY WORLD!
Copy after login

3.Write a program to check the given string title or not:

istitle() checks the string follows the rules of a title.

The above is the detailed content of Day - String functions. 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