How can I print a list that doesn't have the default [] and is changed to capital letters?

Question:

I have a list in python e.g.

letters = ["a", "b", "e"]

And I can print it without the default []:

print(', '.join(letters))

a, b, e

But how can I print out A, B, E so that the strings in the list are all uppercase?

I tried doing print(', '.join(letters).upper) but that didn’t work.

Asked By: TNTDoctor

||

Answers:

You need to apply .upper() to each string in your list:

alist = ["a", "b", "e"]
print(', '.join([c.upper() for c in alist]))

Output as requested.

Update

Alternatively, since .join() produces a string, then .upper() will work on that:

print(', '.join(alist).upper())

Answered By: quamrana

You code does work (with a slight modification), you only need to add () after upper:

print(', '.join(list).upper())

Note: I think another variable name would be better than list since it is the builtin list class.

Answered By: Ebram Shehata

Your code is almost correct, but upper is a method and so it needs () added to call it.

Your code should look like

print(', '.join(list).upper())

Answered By: Adam
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.