Checking String Characteristics: A Python Program with Validation

Verified

Added on  2023/06/04

|3
|765
|395
Homework Assignment
AI Summary
This assignment presents a Python program designed to analyze the characteristics of a user-inputted string. The program utilizes various Python string methods, including `islower()`, `isupper()`, `isalnum()`, and `isalpha()`, to determine if the string contains lowercase letters, uppercase letters, alphanumeric characters, alphabetic characters, and digits. The program incorporates input validation to ensure the string length is less than 50 characters, providing an error message if the input exceeds this limit. The program then outputs the results of these checks to the console. The reflection section includes details on the program's purpose, algorithm, inputs, outputs, and limitations, with a discussion of potential applications in data validation. The program demonstrates the practical application of string methods in Python for validating and analyzing text data, making it useful for various applications where data integrity is important. The program demonstrates the practical application of string methods in Python for validating and analyzing text data, making it useful for various applications where data integrity is important.
Document Page
Task 2: A program to check various characteristics of a String
Introduction
Iam using various methods of Python to check the characterstics of a string like islower,
isupper, isalnum and isalpha.I have made several ways in my program to check whether the string
entered by the user has an alphabet, digit, alphanumeric character or Upper and lower case lettersI
have also implemented Validation handling in my program.I had to read a String from the user, So I
have written a function “readString(),” which will take user input and check that it has a length of
less than 50 or not. If the length of the entered string is greater than 50, then it will print an error
message and ask the user again for the input. All these string characterstics are then printed to the
console window as result.
Enter a String (Less than 50 characters): this is a sample input string whose length is greater than 50
characters.
Length of String must be less than 50 characters.
Enter a String (Less than 50 characters): This is the string.
Has Alphanumeric Letter? True.
Has Alphabetical Letter? True.
Has Digit? False.
Has Lower-case Letter? True.
Has Upper-case Letter? True.
Therefore, the user first entered a string which had a length greater than 50 characters. So,
my program printed an error message and asked the user to input the line again. Once a valid length
string is entered, the results are printed on the screen.
Completed Program
def readString():
inp= input("Enter a String (Less than 50 characters) : ")
if len(inp)>50:
print("Length of String must be less than 50 characters.\n")
return readString();
return inp;
def hasUpperCase(str):
for x in str:
if x.isupper():
return True;
return False;
def hasLowerCase(str):
for x in str:
if x.islower():
return True;
return False;
def hasAlphaNumeric(str):
for x in str:
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
if x.isalnum():
return True;
return False;
def hasAlphabet(str):
for x in str:
if x.isalpha():
return True;
return False;
def hasDigit(str):
for x in str:
if x.isdigit():
return True;
return False;
def main():
st=readString();
print(st)
print("Has Alphanumeric Letter ?" +str(hasAlphaNumeric(st)))
print("Has Alphabetical Letter ?" +str(hasAlphabet(st)))
print("Has Digit ?" +str(hasDigit(st)))
print("Has Lower Case Letter ?" +str(hasLowerCase(st)))
print("Has Upper Case Letter ?" +str(hasUpperCase(st)))
if __name__ == '__main__':
main()
Screenshot
Document Page
Reflection Questions
Project Purpose: To output various attributes of the entered screen to the user. It checks that:
If the string has any Alphanumeric character?
If the string has any Alphabet character?
If the string has any Digit?
If the string has any Lower-case character?
If the string has any Upper-case character?
Algorithm Used: I used the methods of the Python String in my program.
Program Inputs: A string with a length less than 50.
Program Outputs: Program prints the various characteristics of the screen as mentioned in
purpose.
Program Limitations: There are no limitations of this program, and it can be extended to carry out
various other functionalities.
Program Errors: No errors were encountered during testing.
Conclusion
In conclusion, these methods can be used in various application for validation checking.
Furthermore, these methods could be used to check if the user has entered valid data or not. I can
use these methods to check that the name contains any digit or not. The name of a person cannot
have any digits. Hence, I can use such conditions to validate user inputs.
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]