简体   繁体   中英

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","ja@gmail.com","+60123954213","15/5/1990"],["ctm2","jactin","jac@gmail.com","+60123237213","15/5/1990"],["ctm3","jactmartin","jacksontai@gmail.com","+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
| ja@gmail.com
| +60123954213
| 15/5/1990
| ctm2
| jactin
| jac@gmail.com        
| +60123237213
| 15/5/1990
| ctm3
| jactmartin
| jacksontai@gmail.com
| +60166191111
| 15/5/1990

Output with end="" :

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | ja@gmail.com         | +60123954213 | 15/5/1990     | ctm2 | jactin      
| jac@gmail.com        | +60123237213 | 15/5/1990     | ctm3 | jactmartin  | jacksontai@gmail.com | +60166191111 | 15/5/1990

Expecting output:

| ID   | Username    | Email Address        | Phone number | Date of Birth |
| ctm1 | jaconsdsdsd | ja@gmail.com         | +60166197213 | 15/5/1990     |
| ctm2 | jactin      | jac@gmail.com        | +60166197213 | 15/5/1990     |
| ctm3 | jactmartin  | jacksontai@gmail.com | +60166197213 | 15/5/1990     |

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 | ja@gmail.com         | +60123954213 | 15/5/1990     |
| ctm2 | jactin      | jac@gmail.com        | +60123237213 | 15/5/1990     |
| ctm3 | jactmartin  | jacksontai@gmail.com | +60166191111 | 15/5/1990     |

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.

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