我有以下字符串:
Python太棒了......
我真的要告诉你......
永不放弃......
我想用python来计算字符串末尾有多少个点。
功能:
string.endswith(".")
只给我True或False布尔值。有没有办法得到确切的数字?
答案 0 :(得分:0)
如果点只在最后,它应该有所帮助:
string.count('.')
但如果短语不仅在最后有点 - 它需要改变
答案 1 :(得分:0)
你可以使用代码
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;
答案 2 :(得分:0)
你可以这样做:
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)
答案 3 :(得分:0)
你可以这样做
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)
答案 4 :(得分:0)
你可以这样做:
(function() { var base_url = 'www.domain.com' })()