• Register
0 votes
31 views

I am newbee and I was trying some codes in python, I wrote a code in python to count how many times a word appears in a string  :

string = "Not Your buisness surely not your"

CountString = string.count("buisness" 0, 6)
print("CountString")

It shows an error message:

File "main.py", line 3
    CountString = string.count("buisness" 0, 6)
                                          ^
SyntaxError: invalid syntax

why am I getting this type of error message and how can I check how many times a word appears in a string?

10 7 2
6,050 points

1 Answer

0 votes

To count how many times a word appears in string  you used 

count()

method .That was right but you should have write your code:

string = "Not Your buisness surely not your"


print(string.count("Not", 0, 20))
print(string.count("Your", 0, 20))
print(string.count("buisness", 0, 20))
print(string.count("surely", 0, 20))
print(string.count("not", 0, 20))
print(string.count("your", 0, 20))

In this format. You should get an output like:

1
1
1
0
0
0

Happy Coding.

11 6 4
34,930 points

Related questions

1 vote
1 answer 31 views
31 views
Problem: I am new in python programming language and I have been doing a online python course. I am doing my coding practice. I tried to check out in a string how many time word does exist or appear. I wrote this code to check this thing: test = "If you do not ... 3 Appearence = test.count("do" 0, 100) ^ SyntaxError: invalid syntax Can anyone help me out to solve this thing. Thank you in advance.
asked Apr 7 LizzyM 6.1k points
2 votes
1 answer 16 views
16 views
Problem: I just started learning java and solving problems from various online websites. Stuck on a problem saying, count how many times a substring appears in a string java. This problem is beyond my understanding. I found Kodlogs very helpful and friendly environment ... possible solution (with code snippet) would be great for me, and I must recognize your help. Thanks in advance, mate.
asked Mar 25 Gavin 15.3k points
1 vote
1 answer 25 views
25 views
Problem: I have a list that contains the values: list = [2, 4, 8, 16, 32, 2, 64, 128, 2, 256, 512, 2] I wrote a python program for this where I could find out how many times a value appears in a list python: def count_appearence(list, z): check = ... count_appearence(list, z))) NameError: name 'z' is not defined What is the problem with my code. Please help me solve this thing. Thanks in advance.
asked May 13 samhaz 5k points
2 votes
1 answer 12 views
12 views
Problem: I was trying to find the first repeated word in a string in python using dictionary. So I wrote this code which give me error. def whichRepeat(input): words = input.split(' ') dict = words for key in words: if dict[key]>1: print (key) return if __name__ == ... "main.py", line 10, in whichRepeat if dict[key]>1: TypeError: list indices must be integers or slices, not str How to solve this?
asked Apr 28 samhaz 5k points
0 votes
1 answer 22 views
22 views
How to get the number from the occurences of individual characters Supose the string is: x = 'cdfghcfonccn' How can i solve this problem.
asked Mar 10 LizzyM 6.1k points