Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I begin a string with a number in a python program?

I have written the following python project which provides a random output of 20 strings and I have set the limit of it to be less than 1,00,000 during each run and every string shall be unique.

Now I want to add a line of code that helps me start each output with a number

To make it more simple here is an example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Output right now: AtcaF268d1whnkwiwuniw

Output I need: 7atcaF268d1whnkwiwuni

So each output is a random number.

Here is the code to edit

import random

results = []
while len(results) < 100000:
    result_str = ''.join((random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for i in range(20)))
    if result_str not in results:
        results.append(result_str)

with open('string_test.txt','a') as out:
    out.write( '\n'.join(results) )

>Solution :

just gernerate it seperatly i guess?

import random

results = []
while len(results) < 100000:
    result_str = random.choice('0123456789') + ''.join((random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for i in range(19)))
    if result_str not in results:
        results.append(result_str)

with open('string_test.txt','a') as out:
    out.write( '\n'.join(results) )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading