Richard
Richard

Reputation: 37

Python : How to print new line after using end="" in for loop?

I was trying to print out the records of customer in list using for loop, but every item in the list was printed out in every new line. Therefore, I tried to use end="" to print every item on the same line, but how can I print another customer records on next line whenever the the last column of records is printed out ?

customer_list = [["ctm1","jaconsdsdsd","[email protected]","+60123954213","15/5/1990"],["ctm2","jactin","[email protected]","+60123237213","15/5/1990"],["ctm3","jactmartin","[email protected]","+60166191111","15/5/1990"]]

# show records of customer account info
def view_customer_account():
     # initialize column length
     column_length = [2,8,13,12,13]
     # loop for longest data length and make it as column length
     # column length will remain as initial column length if longest data length is shorter than it
     for customer in customer_list:
          for i in range(len(column_length)):
               if len(customer[i]) > column_length[i]:
                    column_length[i] = len(customer[i])
     # print column name
     # column name string will concatenate with a space which multiply with (column length - initial column length)
     print(f'| ID{" "*(column_length[0]-2)} | Username{" "*(column_length[1]-8)} | Email Address{" "*(column_length[2]-13)} | Phone number | Date of Birth |')
     # print records
     # records(str) will concatenate with a space which multiply with (column length/initial column length) - records str length
     for customer in customer_list:
          for i in range(len(column_length)):
               print(f'| {customer[i]+" "*(column_length[i]-len(customer[i]))} ',end="")
     
view_customer_account()

Output without using end="":

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1
| jaconsdsdsd
| [email protected]
| +60123954213
| 15/5/1990
| ctm2
| jactin
| [email protected]        
| +60123237213
| 15/5/1990
| ctm3
| jactmartin
| [email protected]
| +60166191111
| 15/5/1990

Output with end="":

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | [email protected]         | +60123954213 | 15/5/1990     | ctm2 | jactin      
| [email protected]        | +60123237213 | 15/5/1990     | ctm3 | jactmartin  | [email protected] | +60166191111 | 15/5/1990

Expecting output:

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | [email protected]         | +60166197213 | 15/5/1990     |
| ctm2 | jactin      | [email protected]        | +60166197213 | 15/5/1990     |
| ctm3 | jactmartin  | [email protected] | +60166197213 | 15/5/1990     |

Upvotes: 0

Views: 2284

Answers (2)

Kien Nguyen
Kien Nguyen

Reputation: 2691

Add print('|') after the inner for loop to print the last | character of each row and insert a new line:

    for customer in customer_list:
        for i in range(len(column_length)):
            print(f'| {customer[i] + " " * (column_length[i] - len(customer[i]))} ', end="")
        print('|')  # add this one

Output:

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | [email protected]         | +60123954213 | 15/5/1990     |
| ctm2 | jactin      | [email protected]        | +60123237213 | 15/5/1990     |
| ctm3 | jactmartin  | [email protected] | +60166191111 | 15/5/1990     |

Upvotes: 1

Torben Klein
Torben Klein

Reputation: 3116

Just add a print() statement (without any parameters) in the outer loop. One level left of the "innermost" print add the end of your code.

It will just advance the "cursor" to a new line without actually printing anything.

Upvotes: 2

Related Questions