-1

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))
BB956
  • 31
  • 3
  • 11
  • ord function takes only one argument. You can specify ord('A') and that will give you the Unicode point for 'A' which is a one character string. That is why you are getting a syntax error. – Arya Apr 19 '17 at 02:56
  • 5
    A tip of asking a good question: instead of saying *keep getting a syntax error*, show the exact error message. – Yu Hao Apr 19 '17 at 02:57
  • Possible duplicate of [How to print on the same line in Python](http://stackoverflow.com/questions/33905032/how-to-print-on-the-same-line-in-python) – devDeejay Apr 19 '17 at 03:01

4 Answers4

5

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='')
EthanBar
  • 667
  • 1
  • 9
  • 24
  • I believe the end '' is for the print function – TerryA Apr 19 '17 at 02:59
  • This partially works but. But end' ' is there to try to place the words on the same line. The code you gave me prints on different lines and I'm trying to get an output of ABCDEF....... – BB956 Apr 19 '17 at 03:03
  • @BB956 Just updated my answer, you just placed it inside the `odr()` call by accident. – EthanBar Apr 19 '17 at 03:04
1

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!

rcw
  • 99
  • 12
1

Something like this.

import string

for letter in string.ascii_uppercase:print(letter, end=" ")
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

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

Spencer D
  • 3,376
  • 2
  • 27
  • 43