1

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?

1 Answer 1

2

New Response:

I looked at your code and the way your header is formatted, there is some alignment to be done. So try this and it will center itself. For every column, I am subtracting 5 and adding it to the next column. The reason: you have a space of 5 chars between each column. So I have to adjust for that.

print("\n{:^10}{:^20}{:^10}{:^20}"

The alternate to do this is:

print("\n{:^10}     {:^10}     {:^10}     {:^10}"

The output for this are as follows:

# Upper        # Lower        # Digits       # Punct.       
----------     ----------     ----------     ----------     
    1              16             0              1          

    32             16             7              12         

  

Earlier response:

Can you try to give the following to center it:

print("\n{:^15}{:^15}{:^15}{:^15}"

To format the data, use one of these options. By default, strings are left justified and numbers are right justified.

< is for left justified
> is for right justified
^ is for center justified

You can learn more about formatting here

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this formatting but didn't give me the expected results
I made some updates based on your code. See the results now.
why are we adding a space inside the print?
Your text is setup that way so we need to either provide space or use 10,20,10,20 option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.