python - How do I move a specific part of a string to a new string -
For example, suppose I'm using the following string
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
Example = "test" newKey = ""
And I want to get a random section of that string and I know the length. Then
length = lan (example) rand = random.rendant (0, lane (key) - length)
I believe this The code length is less than the random number key from a 0. It should be insured that I always have enough letters, the part I'm stuck on is that how do I use them to take a specific part of the starting point, rand and end point, length, and key. For example, suppose that rand = 2
and length = 4
. Then there should be newKey
, newKey = "MGTD"
.
Which string command can I use to get this result?
You can simply slice the key:
newKey = key [Rand: rand + length]
demo:
& gt; & Gt; & Gt; Length, rand = 4, 2> and gt; & Gt; Key = "XPMGTDHLYONZBWEARKJUFSCIQV"> gt; & Gt; & Gt; Key [rand: rand + length] 'mgtd'
Comments
Post a Comment