added stringfunctions.py

This commit is contained in:
sanju2728 2023-10-01 12:52:36 +05:30
parent 4f7abd17f6
commit 355c9b417c
No known key found for this signature in database

View File

@ -2,8 +2,11 @@
like isupper(), islower() and so on'''
#function to check whether the string is in uppercase
def is_upper(string):
'''
This function checks if a given string contains only upper case characters.
'''
if not string:
return False
for character in string:
@ -11,16 +14,21 @@ def is_upper(string):
return False
return True
#function to check whether the string is in lowercase
def is_lower(string):
'''
This function checks if a given string contains only lower case characters.
'''
for character in string:
if ord(character) in range(65, 97):
return False
return True
#function to check whether all characters in a string are alphabetical
def is_alpha(string):
'''
This function checks whether all characters in a string are alphabetical
'''
if(not string):
return False
for character in string:
@ -30,8 +38,11 @@ def is_alpha(string):
return False
return True
#function to check for only alphanumeric characters
def is_alnum(string):
'''
This function checks if all the characters in a string are alphanumeric
'''
if(not string): return False
for character in string:
if ((ord(character) in range(65, 91)) or (ord(character) in range(97, 123)) or (ord(character) in range(48, 58))):
@ -40,25 +51,34 @@ def is_alnum(string):
return False
return True
#function to check for digits only
def is_decimal(string):
'''
This function returns true if all of the characters in the string are digits
'''
if(not string): return False
for character in string:
if(ord(character) not in range(48, 58)):
return False
return True
#function to check for spaces, tabs and newlines
def is_space(string):
'''
This function checks for the strings constituing only of spaces, tabs and newlines
'''
if(not string): return False
for character in string:
if(character !=' '): #solve for newline pending
if(character != ' '): # solve for newline pending
return False
return True
#function to check whether first letter of each word is capital
def is_title(s):
for substring in s.split():
def is_title(string):
'''
This function checks that every word starts with an uppercase letter.
'''
for substring in string.split():
if(ord(substring[0]) not in range(65, 91)):
return False
for character in range(1, len(substring)):