Home > other >  Ask a python foundation problem
Ask a python foundation problem

Time:09-26

& gt;> X=string. Who + string. Ascii_letters + string. Punctuation
& gt;> Pick=STR. Join ([the random choice (x) for I in range (8)])
Traceback (the most recent call last) :
File "
Pick=STR. Join ([the random choice (x) for I in range (8)])
TypeError: descriptor 'join' for 'STR' objects doesn 't apply to a' list 'object
& gt;> Table=STR. Maketrans (' abcdef123 ', 'sirmvyela)
>
why pick function compilation is not successful, the following table function can succeed?
I look at the format??

CodePudding user response:

STR. Join function using the premise is to set up the first specified characters, to each element in a sequence of connected again!
So before pick assignment statement, you can give the STR assignment
STR='

Then pick=...

CodePudding user response:

Do not conform to the obvious type

CodePudding user response:

1, is your STR is not defined, STR=", "try it?
2, I don't know what is your X, string? , if the string is, should be no problem
S1=[01 ""," 2 ", "3", "01", "2", "3", "01", "2", "3", "01", "2", "3"]
STR=", "
Print (STR. Join (s1))
Print (STR. Join ([the random choice (" adfaf ") for I in range (8)]))

CodePudding user response:

reference 1st floor paullbm response:
STR. Join function using the premise is to set up the first specified characters, to each element in a sequence of connected again!
So before pick assignment statement, you can give the STR assignment
STR='

Then pick=...

Why join the premise is to have a first set of characters? I help STR. Join, not found have this rule?

CodePudding user response:

The
reference 3 floor weixin_45903952 response:
1, is your STR is not defined, STR=", "try it?
2, I don't know what is your X, string? , if the string is, should be no problem
S1=[01 ""," 2 ", "3", "01", "2", "3", "01", "2", "3", "01", "2", "3"]
STR=", "
Print (STR. Join (s1))
Print (STR. Join ([the random choice (" adfaf ") for I in range (8)]))

Right, STR is not defined, but also is not defined, why maketrans can compile successfully, can't the rest of the join?

CodePudding user response:

reference 5 floor Lin_Kayle reply:
right, STR is not defined, but also is not defined, why maketrans can compile successfully, can't the rest of the join?


Look at the two methods described the differences:

The join () method is used to sequence of elements in the with the specified characters connected to generate a new string,
Maketrans () method is used to create the transformation of character mapping table, for the most simple call takes two parameters, the first parameter is the string, said the need to convert the characters, the second parameter is also the goal of string representation transformation, both the length of the string must be the same, for one-to-one relationship,

The join method parameters only one, if you do not specify a character, how to carry out the so-called connections?
And maketrans method already has two parameters, there would be no need to specify the connection character,
  • Related