100

I just started learning Python but I'm sort of stuck right now.

I have hash.txt file containing thousands of malware hashes in MD5, Sha1 and Sha5 respectively separated by delimiters in each line. Below are 2 examples lines I extracted from the .txt file.

416d76b8811b0ddae2fdad8f4721ddbe|d4f656ee006e248f2f3a8a93a8aec5868788b927|12a5f648928f8e0b5376d2cc07de8e4cbf9f7ccbadb97d898373f85f0a75c47f 56a99a4205a4d6cab2dcae414a5670fd|612aeeeaa8aa432a7b96202847169ecae56b07ee|d17de7ca4c8f24ff49314f0f342dbe9243b10e9f3558c6193e2fd6bccb1be6d2

My intention is to display the first 32 characters (MD5 hash) so the output will look something like this:

416d76b8811b0ddae2fdad8f4721ddbe 56a99a4205a4d6cab2dcae414a5670fd

Any ideas?

John Smith
  • 7,243
  • 6
  • 49
  • 61
Rising Lee
  • 995
  • 2
  • 6
  • 5
  • 7
    Read this [brief Introduction to Python](http://docs.python.org/tutorial/introduction.html), and scroll about halfway down. "String slicing" is what you want. – Joel Cornett Jul 30 '12 at 02:48
  • 13
    `the_string[:32]` – JBernardo Jul 30 '12 at 02:48
  • 2
    String slicing and how to open files if you haven't read that already – César Jul 30 '12 at 02:49
  • 2
    You're probably also going to be interested in the `.split()` and `.strip()` methods of strings. For example, `md5, sha1, sha5 = line.strip().split("|")` does pretty much what you'd guess it would. – DSM Jul 30 '12 at 03:07
  • 1
    Thanks for your comments guys I finally got it to work. I tried each and every methods and it seems like TankorSmash's works the most convenient for me. I appreciate each and everyone's help! – Rising Lee Jul 30 '12 at 06:06

3 Answers3

205

You can 'slice' a string very easily, just like you'd pull items from a list:

a_string = 'This is a string'

To get the first 4 letters:

first_four_letters = a_string[:4]
>>> 'This'

Or the last 5:

last_five_letters = a_string[-5:]
>>> 'string'

So applying that logic to your problem:

the_string = '416d76b8811b0ddae2fdad8f4721ddbe|d4f656ee006e248f2f3a8a93a8aec5868788b927|12a5f648928f8e0b5376d2cc07de8e4cbf9f7ccbadb97d898373f85f0a75c47f '
first_32_chars = the_string[:32]
>>> 416d76b8811b0ddae2fdad8f4721ddbe
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
  • 3
    @César: it is generally better (and faster) to show the fact that you only need the first part of the splited string with `the_string.split('|', 1)[0]`. – Eric O. Lebigot Jul 30 '12 at 03:13
18

Since there is a delimiter, you should use that instead of worrying about how long the md5 is.

>>> s = "416d76b8811b0ddae2fdad8f4721ddbe|d4f656ee006e248f2f3a8a93a8aec5868788b927|12a5f648928f8e0b5376d2cc07de8e4cbf9f7ccbadb97d898373f85f0a75c47f"
>>> md5sum, delim, rest = s.partition('|')
>>> md5sum
'416d76b8811b0ddae2fdad8f4721ddbe'

Alternatively

>>> md5sum, sha1sum, sha5sum = s.split('|')
>>> md5sum
'416d76b8811b0ddae2fdad8f4721ddbe'
>>> sha1sum
'd4f656ee006e248f2f3a8a93a8aec5868788b927'
>>> sha5sum
'12a5f648928f8e0b5376d2cc07de8e4cbf9f7ccbadb97d898373f85f0a75c47f'
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
3

If you want first 2 letters and last 2 letters of a string then you can use the following code: name = "India" name[0:2]="In" names[-2:]="ia"

Darshan Jain
  • 781
  • 9
  • 19