How to display the first few characters of a string in Python?

后端 未结 3 1725
南旧
南旧 2021-01-30 19:15

Hi 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 respe

相关标签:
3条回答
  • 2021-01-30 19:50

    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"

    0 讨论(0)
  • 2021-01-30 19:53

    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'
    
    0 讨论(0)
  • 2021-01-30 19:54

    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
    
    0 讨论(0)
提交回复
热议问题