DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

1

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay