0

I need a method to check if there are three o whitespaces a string. Currently I only know how to check if there is a whitespace, with this: if " " in someString:. The reason why I want to find how many whitespaces there are, is because I need to narrow the search down of decrypted messages.

You see, the encrypted message is a long string of random letters and numbers. I use itertools.product("abcdefghijklmnopqrstuvwxyzæøå", repeat=6) to generate a set of keys, assuming the length of the key is six. In order to find the correct original message, which is a setence in english I need to narrow down the search. I figured out that checking for three or more whitespaces in a string is a great way to do so, since a sentence usually consist of multiple whitespaces.

I really want some tips, rather than the solution, since this is a task I want to figure out by myself! :D

a_amundsen
  • 55
  • 6
  • Here's a tip, read what whitespace actually is. – vaultah Mar 29 '15 at 16:11
  • 2
    try `someString.count(' ')` if you are interested only in space characters – inspectorG4dget Mar 29 '15 at 16:12
  • Thanks guys! Should I remove the post? – a_amundsen Mar 29 '15 at 16:14
  • @AndreasAmundsen In your post, replace all instances of the word 'whitespace' with 'space' – jkd Mar 29 '15 at 16:20
  • 1
    "Whitespace" is a generic term and includes tabs ("\t"), new-lines ("\n") and carriage-returns ("\r") - the phrase does not just mean a space character. – cdarke Mar 29 '15 at 16:20
  • Considering your original question, you might consider using regular expressions with the `\b` anchor. This marks word-boundaries, which could include any white-space character, punctuation, and start/end of line. – cdarke Mar 29 '15 at 16:57

1 Answers1

1

If you are actually interested in the exact count of any character that is considered white space, you could use something like this:

s = " nar\trow down the se\narch."
num_whitespace = len([c for c in s if c in [' ', '\n', '\t']])
print(num_whitespace) # 6

Of course, you can add some whitespace characters as needed.

maahl
  • 547
  • 3
  • 17