Python String Report: Analyzing and Reporting String Properties

 InfoTc’1040’Introduction’to’Problem’Solving’and’Programming String’Report ’ 1 In this programming assignment you are going to use the material covered in Chapter 8 More About Strings to write a program called stringreport.py that creates a report about a string entered by the user at a prompt. Requirements’ Request a string from the user using the prompt "Enter a string: " and do the following: Determine the length of the entered string and do one of the following: o If it is zero characters in length output the message "You did not enter anything!" and exit the program. o If the entered string is one or more characters in length display the following information about the string and exit the program. § The length of the string. That is, the number of characters it contains. § The first character of the string. § The last character of the string. § Indicate if the string contains the word "open". § Indicate if the string contains only alphabetic letters and spaces. § Indicate if it contains only numeric digits. § Indicate if the entire string is lower case. § Indicate if the entire string is upper case. Note:



For’the’items’above’that’say’"Indicate’if. ..",’the’ output’is’to’present’a’"yes"’or’"no". The information is to be formatted in reports that look like the ones presented in the following examples. The labels shown are to be used in your program. Enter a string: please open my email Length: 20 First character: p Last character: l Contains open: yes Only alphabetic letters and spaces: yes Only numeric digits: no All lower case: yes All upper case: no Enter a string: You did not enter anything!

Background image of page 1

InfoTc’1040’Introduction’to’Problem’Solving’and’Programming String’Report ’ 2 Enter a string: 8675309 Length: 7 First character: 8 Last character: 9 Contains open: no Only alphabetic letters and spaces: no Only numeric digits: yes All lower case: no All upper case: no Enter a string: MIZZOU Length: 6 First character: M Last character: U Contains open: no Only alphabetic letters and spaces: no Only numeric digits: no All lower case: no All upper case: yes Enter a string: The user selected open 28 times! Length: 32 First character: T Last character: ! Contains open: yes Only alphabetic letters and spaces: no Only numeric digits: no All lower case: no All upper case: no Testing’ Once you have written your program you need to test it. You should run your program and enter a variety of strings to make sure it properly handles generating reports for the different strings. Submission’ Put your stringreport.py file in a folder named <lastname><firstname>StringReport’ and zip the folder. Do not include characters other than a-z and A-Z in the folder name. The zip file is to be submitted for this assignment. If’you’need’additional’help,’post’your’questions’on’the’class’Help’discussion’ board’or’contact’an’eLearning’mentor.


The answer

Description:

This Python program (stringreport.py) prompts the user to enter a string and generates a detailed report of its properties. The program checks if the string is empty, displays its length, first and last characters, and reports on several characteristics such as whether it contains the word "open", contains only alphabetic letters and spaces, consists only of digits, or is entirely lower or upper case. All results are displayed using clear labels and "yes"/"no" answers, matching the required output format.


Here’s a complete solution for stringreport.py that meets all your requirements and matches the output formatting in your assignment.


python

# stringreport.py def main(): s = input("Enter a string: ") if len(s) == 0: print("You did not enter anything!") return # Length print("Length:", len(s)) # First and last character print("First character:", s[0]) print("Last character:", s[-1]) # Contains "open" contains_open = "yes" if "open" in s else "no" print("Contains open:", contains_open) # Only alphabetic letters and spaces alpha_space = all(c.isalpha() or c.isspace() for c in s) print("Only alphabetic letters and spaces:", "yes" if alpha_space else "no") # Only numeric digits numeric_only = s.isdigit() print("Only numeric digits:", "yes" if numeric_only else "no") # All lower case print("All lower case:", "yes" if s.islower() and len(s) > 0 else "no") # All upper case print("All upper case:", "yes" if s.isupper() and len(s) > 0 else "no") if __name__ == "__main__": main()

How It Works

  • Prompts the user for input.

  • Handles the empty string case.

  • Displays the required properties, each with a label and "yes"/"no" where appropriate.

  • Checks for "only alphabetic letters and spaces" using a generator expression.

  • Checks for numeric only, all lowercase, and all uppercase using built-in string methods.


Test your program with the sample inputs in the assignment to confirm the output matches exactly!


📩 Need a similar solution? Email me: adel455@hotmail.com



Background image of page 2
Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming