Quarismo Quarismo - 4 years ago 277
Python Question

String concatenation in Python 2.7

For an assignment of mine, I have to write a Python program to crack a locked PDF file. The password policy for this file is as follows:


The password is of the form of w1w2w3 where w1,w2 and w3 are three consecutive words, taken from a 50,0000 word story without “e”, concatenated with any o’s replaced with “0” (zero)


So far I have managed to write a program that pulls three random words from the 50,000 word story text file. My code for this is:

from random import shuffle
data = open('randomwords.txt', 'r').read().split()
shuffle(data)

for x in data[:3]:
print x


This programs prints 3 random words on separate lines when run. But I need the program to print the three words on the same line, with no spaces, and with all the o's replaced with 0's

I have tried a few things so far, none of which worked, could use some advice on how to concatenate the strings, and replace the o's with 0's

Answer Source

I have modified one answer posted here, Actually i wanted to edit that answer but deleted by the author.
Try Following:

res = ""
for x in data[:3]:
    res += x
    res.replace("o", "0")
print res

OR

res = ""
for x in data[:3]:
    res = res + x
print res.replace("o", "0")

Try Last one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download