Write code that examines a string stored in a variable called product_id and prints an appropriate version of the id. If the id is composed of only numeric digits, print it as is. If it is composed of a combination of alphabetic letters and digits, print the id with all alphabetic letters in uppercase. Otherwise (if there are any other type of characters in the id), print it with all alphabetic letters in lowercase. Assume that the product_id has already been initialized.

Respuesta :

Answer:

Here is the Python program:

product_id="#12345Aab" #product_id has already been initialized

def StringExaminer(product_id ):   #method that takes product_id as parameter to return appropriate version of product_id      

   if product_id.isdigit():   #if product_id contains digits only

     return product_id  #returns product_id as it is

   elif any(not c.isalnum() for c in product_id):  #if id has special characters

     return product_id.lower()   #print id with letters in lowercase

   else:  #if id is composed of alphabetic letters and digits

     return product_id.upper()  #print the id with all letters in uppercase

print(StringExaminer(product_id)) #calls StringExaminer method by passing product_id to print the appropriate version of id

Explanation:

The program has a method StringExaminer that takes product_id as argument.

Suppose the string is "#12345Aab" So

product_id=  "#12345Aab"

First if condition i.e.  if product_id.isdigit(): uses method isdigit() which returns true if all the characters in product_id are digits. Here this condition evaluates to false as all characters are not only digits. So the program moves to the elif part

elif any(not c.isalnum() for c in product_id):  this elif condition checks if the product_id is not a combination of alphabets and letters. It used isalnum() method returns true if all the characters in product_id are alphanumeric which means the string contains alphabet letters and digits. The logical operator not is also used which checks if isalnum() is not true. The for loop is used to iterate through each character of product_id to check if any character is a special character. So this condition evaluates to true because there is a special character in product_id that is #. So this elif part executes which has the following statement:

return product_id.lower() This statement used lower() method that converts the product_id letters to lower case so it prints product_id with all the alphabetic letters in lower case. Hence when the method StringExaminer is called and passed product_id = "#12345Aab" then the output is:

#12345aab  

The program along with its output is attached.

Ver imagen mahamnasir

The code that examine  a string stored in a variable called product_id and prints an appropriate version of the id, If the id is composed of only numeric digits, print it as it is, If it is composed of a combination of alphabetic letters and digits, print the id with all alphabetic letters in uppercase. Otherwise (if there are any other type of characters in the id), print it with all alphabetic letters in lowercase is as follows:\

product_id  = "1234abc#"

if product_id.isdigit():

  print(product_id)

elif product_id.isalnum():

  print(product_id.upper())

else:

  print(product_id.lower())

Code explanation:

  • The variable product_id is used to store the users string
  • If the string is digit only, it will print the string
  • else if the string contains letters and digit it will print the uppercase of the string.
  • else it will print the lower case of the string.

Note the code is written in python.

learn more on python code here: https://brainly.com/question/10897239

Ver imagen vintechnology