0

I want to find the position of '.', but when i run code below:

text = 'Hello world.'
pattern = '.'
search = re.search(pattern,text)
print(search.start())
print(search.end())

Output is:

0
1

Place of '.' isn't 0 1. So why is it giving wrong output?

HelloBT
  • 3
  • 1

1 Answers1

2

You can use find method for this task.

my_string = "test"
s_position = my_string.find('s')
print (s_position)

Output

2

If you really want to use RegEx be sure to escape the dot character or it will be interpreted as a special character.

The dot in RegEx matches any character except the newline symbol.

text = 'Hello world.'
pattern = '\.'
search = re.search(pattern,text)
print(search.start())
print(search.end())
Pitto
  • 8,229
  • 3
  • 42
  • 51