0

I'm new to Python and am having trouble with a seemingly simple objective. I've tried several methods for splitting a string by a tab character to no avail.

testStr = 'word1    word2'
    values = testStr.split("\t")
    print(values)

Results in

['word1    word2']

and

import re
testStr = 'word1    word2'
print(str(re.search(r'[^\t]', testStr).start()))

results in

0

Used the answers from the following posts:

How to get the first word in the string

splitting a string based on tab in the file

Any help appreciated.

W.Harr
  • 303
  • 1
  • 4
  • 15
  • Are you sure that your test string has a tab, and not spaces? The two are represented differently. I'd imagine a test string of `"word1\tword2"` would split correctly. – roelofs Nov 21 '17 at 02:57
  • @roelofs You are correct. I thought that literally clicking tab when creating the string would be accounted for as \t. – W.Harr Nov 21 '17 at 15:27

1 Answers1

0

Your testStr doesn't start with \t, so in the regexp pattern you do not need a ^.

So in your case it will be:

import re
testStr = 'word1\tword2'
print(str(re.search(r'[\t]', testStr).start()))

And the result will be

5
Hayk Davtyan
  • 797
  • 6
  • 7