Python Tutorial: Random Captcha Generation and Join Strings

Function: string.join()

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

join(): Concatenates an array of strings. Concatenate elements in strings, tuples, and lists with specified characters (delimiters) to generate a new string
os.path.join(): Return after combining multiple paths

1. Function description

1. join() function

Syntax: 'sep'.join(seq)

Parameter Description

  • sep: Separator. can be empty
  • seq: sequence of elements to concatenate, string, tuple, dictionary

The above syntax is: with sep as the delimiter, combine all elements of seq into a new string

Return value: Returns a string generated by concatenating the elements with the separator sep

2. os.path.join() function

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

Return value: Return after combining multiple paths

Note: Arguments before the first absolute path will be ignored

#对序列进行操作(分别使用' '与':'作为分隔符)
  
>>> seq1 = ['hello','good','boy','doiido']

>>> print ' '.join(seq1)

hello good boy doiido

>>> print ':'.join(seq1)

hello:good:boy:doiido

    
#对字符串进行操作
  
>>> 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
    

#对元组进行操作  
>>> seq3 = ('hello','good','boy','doiido')
>>> print ':'.join(seq3)
hello:good:boy:doiido
    
#对字典进行操作
  
>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print ':'.join(seq4)
boy:good:doiido:hello

#合并目录  
>>> import os
>>> os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'

Random verification code generation

import random
li = []
for i  in range(6):
    r = random.randrange(0,5)
    print(r)
    if r == 2 or r ==4:
        num = random.randrange(0,10)
        li.append(str(num))
    else:
        tmp = random.randrange(65,91)
        c = chr(tmp)
        li.append(c)

    
print(li)
re = "".join(li)    
print(re)

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/124256783