Reputation: 21
I am currently trying to figure out a coding issue where I receive a space at the end of my output when I am using ord. If anyone could please help me to fix this error in my code, I will be very grateful.
x = input("Dessert idea: ")
for i in x:
print(ord(i),end=" ")
The output always spits out space at the end of the code along with the numbers, eg:
89 101 101 116 115" "
*Space is shown using quotation marks *
Upvotes: 1
Views: 70
Reputation: 27283
An alternative solution that makes the code look cleaner IMO:
x = input("Dessert idea: ")
print(*(ord(i) for i in x))
This unpacks the calls to ord
into individual arguments, which by default get seperated by spaces, and finishes the line with a newline.
If you're more the functional type, you might prefer print(*map(ord, x))
Upvotes: 1
Reputation: 2981
Try using the following code to prevent the last space on the print line;
x = input("Dessert idea: ")
for i in x[:-1]:
print(ord(i),end=" ")
print(ord(x[-1]))
This loops through all elements except the last, then adds the last element to the print statement without the space.
Upvotes: 1