DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 20 - 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')
Enter fullscreen mode Exit fullscreen mode
Contains fruit
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
Contains Python
0 5

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
Starts with Python
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
Starts with Apple
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
Ends with Python
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
Ends with fruit
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
All are alphabets
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
Not all are alphabets and numbers
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
All are lowercase alphabets
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode
All are uppercase alphabets
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
All are spaces
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
hello, and welcome to my world!
Enter fullscreen mode Exit fullscreen mode

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='')
Enter fullscreen mode Exit fullscreen mode
HELLO, AND WELCOME TO MY WORLD!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)