I'm trying to print all capital letters of the alphabet on the same line, but I keep getting a syntax error.
for c in range(0, 26):
print(chr(ord('A', end '')+c))
I'm trying to print all capital letters of the alphabet on the same line, but I keep getting a syntax error.
for c in range(0, 26):
print(chr(ord('A', end '')+c))
ord()
takes a single character, and converts it into it's Unicode code point. It only takes one parameter. It looks like you meant to place , end ''
in the print()
call. Here's the updated code:
for c in range(0, 26):
print(chr(ord('A')+c), end='')
There is an inbuild function called string for you to import
In order to print all the alphabets with uppercase you can do this:
import string
print string.ascii_uppercase
If you want to put a space in between the letters you can do this:
import string
line = ""
for i in list(string.ascii_uppercase):
line = line + i + " "
print line
Hope it helped!
Something like this.
import string
for letter in string.ascii_uppercase:print(letter, end=" ")
Seems like most of the answers on this have individual pieces correct, but not the whole solution.
I would recommend:
result = ""
for c in range(0, 26):
result += chr(ord('A')+c)
print(result)
The issue is two-fold. Syntax issue is caused by , end ''
as a parameter to ord (it only takes one parameter). Printing across multiple lines is caused by Python's built-in print function always appending new lines. To resolve that, you can just store the results in a string and print that final string when you are done building it. You could also consider using an array such as ArrayVariableName.append(chr(ord('A')+c))
, and then print(''.join(ArrayVariableName))
. (I haven't tested that, but it should work.)