Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of Python random CAPTCHA code generation and join string

Shulou Source: shulou.com Published: 2022-05-31 20:15:17 09月11日 Update

This article mainly introduces "how to solve the problem of Python random CAPTCHA generation and join string". In daily operation, I believe many people have doubts about how to solve the problem of Python random CAPTCHA generation and join string. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the problem of "Python random CAPTCHA generation and join string problem". Next, please follow the editor to study!

Function: string.join ()

There are two functions in Python: join () and os.path.join (). The specific functions are as follows:

Join (): connect an array of strings. Concatenate strings, tuples, and elements in the list with specified characters (delimiters) to generate a new string

Os.path.join (): returns after combining multiple paths

1. Function description 1. Join () function

Syntax: 'sep'.join (seq)

Parameter description

Sep: delimiter. Can be empty

Seq: sequence of elements, strings, tuples, dictionaries to connect

The above syntax is to merge all the elements of seq into a new string with sep as the delimiter

Return value: returns a string generated after concatenating the elements with the delimiter sep

2. Os.path.join () function

Syntax: os.path.join (path2 [, path3 [, …]])

Return value: return after combining multiple paths

Note: parameters before the first absolute path will be ignored

# manipulate the sequence (using''and':'as delimiters respectively) > seq1 = ['hello','good','boy'' 'doiido'] > print' '.join (seq1) hello good boy doiido > print': '.join (seq1) hello:good:boy:doiido # operate on strings > seq2 = "hello good boy doiido" > print': '.join (seq2) h:e:l:l:o:: g:o:o:d:: b:o:y:: d:o:i:i:d:o# operate on tuples > seq3 = (' hello','good','boy' 'doiido') > print': '.join (seq3) # operate on dictionary > seq4 = {' hello':1,'good':2,'boy':3,'doiido':4} > print': '.join (seq4) boy:good:doiido:hello# merge directory > import os > os.path.join (' / hello/','good/boy/','doiido')'/ hello/good/boy/doiido'

Random CAPTCHA generation

Import randomli = [] for i in range (6): r = random.randrange (0je 5) print (r) if r = = 2 or r = = 4: num = random.randrange (0je 10) li.append (str (num)) else: tmp = random.randrange (65pm 91) c = chr (tmp) li.append (c) print (li) re = ".join (li) print (re)

Let's take a look at the random CAPTCHA generated by python.

Train of thought:

1. Add an empty list

2. Add the characters of ASCII table to the empty list

3. Generate 6 random numbers from the list

4. Splice the generated list into a string and output it.

Import random,string# method 1li_code = [] for i in range (65Power91): # uppercase letters Amurz li_code.append (chr (I)) for j in range (97123): # lowercase letters Amurz li_code.append (chr (j)) for k in range (48pl 58): # digits 0-9 li_code.append (chr (k)) code = random.sample (li_code,6) ran_code = ".join (code) print (ran_code) import random String# method 2str1 = "0123456789" str2 = string.ascii_lettersstr3 = str1+str2code = random.sample (str3,6) l_code = ".join (code) print (l_code) import random,string# method 3s_code = string.ascii_letters+string.digitscode = random.sample (slack code 6) print (" .join (code)) so far The study on "how to solve the problem of Python random CAPTCHA generation and join string" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Characters strings generation validation problems functions delimiters elements methods learning syntax paths parameters multiple dictionaries letters sequences more help combination Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Shulou Technology macOS Docker