This question already has answers here:
How do I create an incrementing filename in Python?
(13 answers)
convert number to formatted string in python with prefix 0s
(3 answers)
Closed 2 years ago.
proId = 'nam001'
I want to increment numeric values only. That should be like this 'nam002', 'nam003'.
How to do this?
Just because you asked nicely :D
You can use the simplest for loop, with str.zfill to pad with zeroes, then add it to the 'nam' prefix like this:
for i in range(1,11):
proId = 'nam' + str(i).zfill(3)
print(proId) # or do whatever you need with it...
Output:
nam001
nam002
...
nam009
nam010
Related
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2
This question already has answers here:
Sort list of strings by integer suffix
(4 answers)
Is there a built in function for string natural sort?
(23 answers)
Closed 1 year ago.
I have a list of ['rs12345','rs21','rs55189'];
I need to sort them as numbers after strip the prefix 'rs'.
How can I do it in Python ?
# row.keys() is ['rs12345','rs21','rs55189']
fieldnames = sorted(list(row.keys()),key=itemgetter(slice(2, None)))
This code will not working after add int(''.join(xxx)).
And the dict row is a generator so I have to put it into list() to get its values.
_fieldnames = list(row.keys())
_fieldnames.remove(sidname)
_fieldnames = sorted(_fieldnames, key=lambda i: int(i[2:]))
Got it working.
I forgot that I have to remove sampleid, which contains no ^rs, first.
This question already has answers here:
How to print a number using commas as thousands separators
(30 answers)
Add commas into number string [duplicate]
(11 answers)
What's the easiest way to add commas to an integer? [duplicate]
(4 answers)
Adding thousand separator while printing a number [duplicate]
(2 answers)
Closed 2 years ago.
A beginner here! Can I ask how to split a number string in every 3rd digit from the right and put a comma in between and do it again.
>>>num = '12550'
how can I make that into this
>>>12,550
You can use this neat trick:
num = 123456789
print ("{:,.2f}".format(num))
which outputs:
123,456,789.00
If you don't want the decimal places just use:
print ("{:,}".format(num))
This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.
This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 9 years ago.
I have a python list like so:
list = []
for loop
list.append("blah")
What I want to do it convert this list to string with each value separated by comma.
How do I do this??
Use the str.join method:
','.join(your_list)