Home > Enterprise >  How a create a random string combination of postive, Negative, floats, Upper case, Lower case elemen
How a create a random string combination of postive, Negative, floats, Upper case, Lower case elemen

Time:11-04

I'm new to python seeking your help. I would like to create a string combination of postive, Negative, floats, Upper case, Lower case elements

Example: Like random combination.

As-1jP0.7M -->output

Explanation

A - caps A 
s- small S
-1 - Negative 1 
j- small j
0.7 - float 0.7
M- caps M 

My ranges

Caps - A - Z
small- a -z
positive - 0 to 9
Negatives - -1 to -9
float - 0.1 to 0.9

I know I'm asking too much but by doing some basic researcg I got idea how to generate combination of Alphanumeric numbers like.

import random, string
x = ''.join(random.choice(string.ascii_uppercase   string.ascii_lowercase   string.digits) for _ in range(10))
print(x)

This ok...But, I'm completely clueless how to add Negative & float types along with alphanumerics..Any suggestions how to achieve it. Like we have any some shorcuts like string.floatdigits? or string.negatives? I've searched for similar syntax but, But I 'havent found anything

CodePudding user response:

You can genarate spearate lists according to your ranges and use accordingly.

  1. genarate spearate lists according to your ranges
  2. Merge them - Refernce
  3. Generate random item n number of times & Join as str - Reference
import string
import random

list_small=list(string.ascii_lowercase)
list_cap=list(string.ascii_uppercase)
list_dig = list(range(9,-10,-1)) #both postive & Neg
floats= [ round(x * 0.1, 1) for x in range(1, 10)]

#Merge list    
all_list = list_small   list_cap   list_dig  floats
s =''.join(str(x) for x in ([random.choice(all_list) for i in range(10)]))

print(s)

#output

ircZC-1bj0.7k

CodePudding user response:

Try this:

import random, string
x = ''.join(random.choice(list(string.ascii_uppercase)   
                          list(string.ascii_lowercase)   
                          ['0'] [ch dig for dig in string.digits[1:] for ch in ['', '-', '0.'] ]) 
            for _ in range(10))
print(x)

Random output:0.3z0.7Q0.52-9Ghe

Another random output: Op-7-1R0.8M7wz

  • Related