Formatting list of n-length in SQL friendly string

Question:

I want to be able to enter a list OF ANY LENGTH consisting of 6-digit GLs. Needs to be variable length list for future development.

Reformatting:

Add leading 0000s because old SAP just loves random extra zeros.
Convert the list into the proper formatted string for SQL query.
Saved as a variable to use in an f-string later on.

I can either get the list to the proper length, or with the correct kind of brackets. But, not both.

GLs_Entered = 101010, 202020, 303030, 404040

SQL_GLs = [f'0000{e}' for e in GLs_Entered]

Output_Variable = "('{}','{}','{}')".format(*SQL_GLs)

print(SQL_GLs)
print(output)

OUTPUT OF ABOVE

Need this length     SQL_GLs = ['0000101010', '0000202020', '0000303030', '0000404040']
Need this format     Output_Variable = ('0000101010','0000202020','0000303030')

The SQL GLs is properly adjusting for variable length of the list, unfortunately output is limited by the {}s needing to be defined ahead of time.

Correct answer will be:

('0000101010', '0000202020', '0000303030', '0000404040')

SQL needs the parentheses, won’t accept square brackets as valid (at least in the version I’m forced to use).

Asked By: jb2987

||

Answers:

You can directly get this output by:

output=tuple(f'0000{e}' for e in GLs_Entered)

print(output)
#('0000101010', '0000202020', '0000303030', '0000404040')

Edit:

you can zfill() as commented by @josh

tuple(str(e).zfill(9) for e in GLs_Entered)
#('0000101010', '0000202020', '0000303030', '0000404040')
Answered By: God Is One