I wanted to create a numpy array with same number of digits in each element. Suppose:
User_001, User_002,....,User_123
How can I do this?
I tried as follows:
a1 = np.array([f'User_{i}' for i in range(124)])
But it gives me:
User_1, User_2, User_3......,User_123
which I do not want. Any help?
Asked
Active
Viewed 60 times
-1

zillur rahman
- 355
- 1
- 13
-
4Maybe this will help: https://stackoverflow.com/questions/6869999/fixed-width-number-formatting-python-3/56311800. – Tom S Jun 29 '21 at 08:59
2 Answers
2
here you go
import numpy as np
np.array([f'User_{str(i).zfill(3)}' for i in range(124)])

Ali Sadeghi Aghili
- 524
- 1
- 3
- 15
1
Not the most elegant solutions, but should do well.
a1 = np.array([f'User_{str(i).rjust(3, str(0))}' for i in range(124)])
# or
a1 = np.array([f'User_{str(i).zfill(3)}' for i in range(124)])

A.M. Ducu
- 892
- 7
- 19