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.
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())
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.
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())
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.
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())
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.
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())