I have a simple program that takes a sentence and calculates the number of lowercase, uppercase, digits and punctuation characters. I need to output into a format like this:
# Upper # Lower # Digits # Punct.
-------- -------- -------- --------
2 36 4 5
However, in my code, I have combined the headers, bars and the counts into a separate list. Here is my code:
#Prompting user to enter a sentence
input_string = input("Please enter a sentence: ")
#Initializing variables
lowercase_count = 0
uppercase_count = 0
punctuation_count = 0
digits_count = 0
#Iterating through the string to get the counts
for str in input_string:
if str.isupper():
uppercase_count +=1
elif str.islower():
lowercase_count +=1
elif str in (".", "?", '!', ",", ";", ":", "-", "\'" ,"\""):
punctuation_count +=1
elif str.isnumeric():
digits_count +=1
header_list = ["# Upper", "# Lower", "# Digits", "# Punct."]
bars_list = ['----------']*4
counts_list = [uppercase_count
, lowercase_count
, digits_count
, punctuation_count]
comb_list = [header_list, bars_list, counts_list]
for list in comb_list:
print("{:15}{:15}{:15}{:15}".format(list[0], list[1], list[2], list[3]))
Which gives me an output like this:
# Upper # Lower # Digits # Punct.
---------- ---------- ---------- ----------
1 16 0 1
If I were to print out the header, list and counts separately, I can control the align parameter
#Printing Header
print("{:15}{:15}{:15}{:15}"
.format(header_list[0], header_list[1], header_list[2], header_list[3]))
#Printing bars
for header in header_list:
print("{:15}".format("----------"), end="")
#Printing values
print("\n{:5}{:15}{:15}{:15}"
.format(uppercase_count
, lowercase_count
, digits_count
, punctuation_count))
which gives me the expected output:
# Upper # Lower # Digits # Punct.
---------- ---------- ---------- ----------
1 16 0 1
How do I control the align parameter when iterating through list of lists? What's the best way to print the output?