简体   繁体   中英

Outputs printed on same line - python

message = str(input())
for i in message:
  if i == " ":
    print(" ")
  else:
    # ord(i) returns the ASCII number for i
    # To get the actual alphabetical position of i, you have to do ASCII of letter - ASCII of A + 1.
    print(ord(i)-ord("a")+1)

This program converts each character of the users input to a letter, based on their alphabetical order (a = 1, b = 2, ect) and prints each number output on a new line. How do I get change this so that when each number is printed, it is on the same line as the last? (eg 123)

Set the optional end argument to "" in print() as:

print(ord(i)-ord("a")+1, end="")

Use this print("what ever you want to print",end= " ")

It will print without taking the cursor down a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM