9

I'm trying to analyze the contents of a string. If it has a punctuation mixed in the word I want to replace them with spaces.

For example, If Johnny.Appleseed!is:a*good&farmer is entered as an input then it should say there are 6 words, but my code only sees it as 0 words. I'm not sure how to remove an incorrect character.

FYI: I'm using python 3, also I can't import any libraries

string = input("type something")
stringss = string.split()

    for c in range(len(stringss)):
        for d in stringss[c]:
            if(stringss[c][d].isalnum != True):
                #something that removes stringss[c][d]
                total+=1
print("words: "+ str(total))
Jan Z.
  • 6,883
  • 4
  • 23
  • 27
Harry Harry
  • 179
  • 2
  • 2
  • 8

7 Answers7

15

Simple loop based solution:

strs = "Johnny.Appleseed!is:a*good&farmer"
lis = []
for c in strs:
    if c.isalnum() or c.isspace():
        lis.append(c)
    else:
        lis.append(' ')

new_strs = "".join(lis)
print new_strs           #print 'Johnny Appleseed is a good farmer'
new_strs.split()         #prints ['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']

Better solution:

Using regex:

>>> import re
>>> from string import punctuation
>>> strs = "Johnny.Appleseed!is:a*good&farmer"
>>> r = re.compile(r'[{}]'.format(punctuation))
>>> new_strs = r.sub(' ',strs)
>>> len(new_strs.split())
6
#using `re.split`:
>>> strs = "Johnny.Appleseed!is:a*good&farmer"
>>> re.split(r'[^0-9A-Za-z]+',strs)
['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
11

Here's a one-line solution that doesn't require importing any libraries.
It replaces non-alphanumeric characters (like punctuation) with spaces, and then splits the string.

Inspired from "Python strings split with multiple separators"

>>> s = 'Johnny.Appleseed!is:a*good&farmer'
>>> words = ''.join(c if c.isalnum() else ' ' for c in s).split()
>>> words
['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
>>> len(words)
6
Community
  • 1
  • 1
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
3

try this: it parses the word_list using re, then creates a dictionary of word:appearances

import re
word_list = re.findall(r"[\w']+", string)
print {word:word_list.count(word) for word in word_list}
Dotan
  • 6,602
  • 10
  • 34
  • 47
3

How about using Counter from collections ?

import re
from collections import Counter

words = re.findall(r'\w+', string)
print (Counter(words))
sweet_sugar
  • 1,390
  • 3
  • 13
  • 22
1
for ltr in ('!', '.', ...) # insert rest of punctuation
     stringss = strings.replace(ltr, ' ')
return len(stringss.split(' '))
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
1

I know that this is an old question but...How about this?

string = "If Johnny.Appleseed!is:a*good&farmer"

a = ["*",":",".","!",",","&"," "]
new_string = ""

for i in string:
   if i not in a:
      new_string += i
   else:
      new_string = new_string  + " "

print(len(new_string.split(" ")))
TMoover
  • 620
  • 1
  • 7
  • 22
0
#Write a python script to count words in a given string.
 s=str(input("Enter a string: "))
 words=s.split()
 count=0
  for word in words:
      count+=1

  print(f"total number of words in the string is : {count}")