0

I need to write a code which recognizes special names such as names for places and people cities and etc. and then it should announce the place of the word and not character. for example if the string is: 'Today Sam and Martin are happy' it should print out this: 1:Sam 3:Martin but my code writes: 0:Today 6:Sam 14:Martin here is the actual code:

import re
b=[]
x=str(input())
r =re.compile('([A-Z][a-z]+)')
a=re.findall(r, x) 
a=list(a)
word=len(x.split())
b=[word for word in range(len(x)) if x[word].isupper()]
o=str([j for i in zip(b,a) for j in i])
d=o.replace(", '", ':').replace("'",'').replace(",", '\n').replace('[','').replace(']', 
'').replace(' ', '')
print(d)

do you know how can I fix it?

  • `it should print out this: 1:Sam 4:Martin` ... _Why_ should this be printed? Can you add an explanation to your question? – Tim Biegeleisen Sep 18 '22 at 07:22
  • `[print(str(i+1) + ":" + j, end=" ") for i,j in enumerate(x.split())]` – Alexander Sep 18 '22 at 07:29
  • To add any other explanation I should say that 'Sam' is the second word and 'Martin' is the fourth word because python's counting function starts counting from 0. that's why it should be counted like that. Also,Thank you Alexander for the help. – Avid Programmer Sep 18 '22 at 11:19

0 Answers0