Can I use multiple variables when using Python format characters?

Asked 2 years ago, Updated 2 years ago, 12 views

When you use a repeating statement in Python, there are multiple values in one variable I wonder if it is possible to use the format function specified by one variable when printing.

import sqlite3

class test():
    def __init__(self):
        self.conn = sqlite3.connect('test.db')
        self.curosr = self.conn.cursor()

        self.conn.execute('create table if not exists test1(id integer primary key autoincrement, name char(5), age char(5))')
        self.conn.execute('create table if not exists test2(id integer primary key autoincrement, name char(5), age char(5))')
        self.conn.execute('create table if not exists test3(id integer primary key autoincrement, name char(5), age char(5))')

        self.conn.execute("insert into test1(id, name, age) values(null, 'andy', '29')")
        self.conn.execute("insert into test1(id, name, age) values(null, 'terry', '30')")

        self.conn.execute("insert into test2(id, name, age) values(null, 'bab', '11')")
        self.conn.execute("insert into test2(id, name, age) values(null, 'john', '24')")

        self.conn.execute("insert into test3(id, name, age) values(null, 'intel', '100')")
        self.conn.execute("insert into test3(id, name, age) values(null, 'amd', '50')")

        for i in range(1, 4):
            for j in self.curosr.execute('select *from test{0} order by id desc'.format(i)).fetchmany():
                dic={'j{}'.format(i):j}

            print("{0},{1},{2}   {3},{4},{5}   {6},{7},{8}".format(j[0],j[1],j[2],j[0],j[1],j[2],j[0],j[1],j[2]))

test()

-----------print----------
2,terry,30   2,terry,30   2,terry,30
2,john,24   2,john,24   2,john,24
2,amd,50   2,amd,50   2,amd,50

In the example code, print ("{0},{1},{2}{3},{4},{5}{6},{7},{8}".format(j[0],j[1],j[2],j[0],j[0],j[1],j[2],j[0],j[1],j[2]]) When outputting is changed to make the output 2,terry,302,john, 2,24> in such a way? I'm curious.

python

2022-09-21 23:19

1 Answers

It's possible

print("{0}, {1}, {2}  {0}, {1}, {2}, {0}, {1}, {2}" .format(j[0],j[1],j[2]))

You can do it like this


2022-09-21 23:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.