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')
Contains fruit
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')
Contains Python 0 5
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')
Starts with Python
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)
Starts with Apple
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')
Ends with Python
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)
Ends with fruit
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')
All are alphabets
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')
Not all are alphabets and numbers
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')
All are lowercase alphabets
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')
All are uppercase alphabets
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')
All are spaces
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='')
hello, and welcome to my world!
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='')
HELLO, AND WELCOME TO MY WORLD!
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!