Skip to content
Advertisement

Convert range(r) to list of strings of length 2 in python

I just want to change a list (that I make using range(r)) to a list of strings, but if the length of the string is 1, tack a 0 on the front. I know how to turn the list into strings using

JavaScript

but I want to be able to also change the length of those strings.

Input:

JavaScript

Output:

JavaScript

And if possible, my final goal is this: I have a matrix of the form

JavaScript

and I want to make a set of strings such that the first 2 characters are the row, the second two are the column and the third two are ’01’, and have matrix[row,col] of each one of these. so the above values would look like such:

JavaScript

Advertisement

Answer

Use string formatting and list comprehension:

JavaScript

or format:

JavaScript
Advertisement