-3

I have strings such as:

Python is awesome...

I really have to tell you ......

never give up..

I would like to use python to count how many dots are there exactly at the end of the string.

The function:

string.endswith(".")

Only gives me True or False boolean value. Is there a way to get the exact number?

KerryChu
  • 25
  • 8

5 Answers5

0

If dots will be only in the end, it should help:

string.count('.')

But if phrase has dots not only in the end - it needs to change

amarynets
  • 1,765
  • 10
  • 27
0

you can use the code

import re

regex = r"\.+(?=\.*)$"

test_str = ("Python is awesome...")

matches = re.finditer(regex, test_str, re.MULTILINE)
count = 0;

for matchNum, match in enumerate(matches):
    matchNum = matchNum + 1

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    count = count + match.end() - match.start();
print count;
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

You can do it like this:

counter = 0
while True:
    if string.endswith('.'):
        counter += 1
        string = string[:-1]  # here we remove the last character
    else:
        break
print("number of dots at the end: " + counter)
Honza Zíka
  • 495
  • 3
  • 12
0

You can do like this

import re
text = '''
Python is awesome...
I really have to tell you ......
never give up..
'''
out = re.findall('\.+$', text)
for o in out:
    print len(o)
Mohsun
  • 11
  • 3
0

You can do it like this:

def get_dots_num(line):
    count=0
    for i in range(len(line)):
        if line[(i*-1)] == '.':
            count+=1
    return count

if __name__ == "__main__":

    line = "hello......"
    if(line.endswith('.')):
        print str(get_dots_num(line))
Honza Zíka
  • 495
  • 3
  • 12
itaisls9
  • 60
  • 4