• Register
1 vote
41 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 do this you have to do this again"

Appearence = test.count("do" 0, 100)
print("Appearence")

but it does not compile and gives the error message:

  File "str.py", line 3
    Appearence = test.count("do" 0, 100)
                                 ^
SyntaxError: invalid syntax

Can anyone help me out to solve this thing. Thank you in advance.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Answer:

“count” method is absoloutly perfect to count this hing but your logics are not right in the programm. 

So write your code as:

test = "If you do not do this you have to do this again"

print(test.count("If", 0, 100))
print(test.count("you", 0, 100))
print(test.count("do", 0, 100))
print(test.count("not", 0, 100))
print(test.count("this", 0, 100))
print(test.count("have", 0, 100))
print(test.count("to", 0, 100))
print(test.count("again", 0, 100))

The given codes output will be:

1
2
3
1
2
1
1
1

Hope you understand the thing and it helps you.

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 70 views
70 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 ... ^ 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?
asked Mar 18, 2020 LizzyM 6.1k points
2 votes
1 answer 41 views
41 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, 2020 samhaz 5k points
1 vote
1 answer 7 views
7 views
Problem: Please help me. How can I solve it? python count how many times a word appears in a string
asked Mar 30 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 8 views
8 views
Problem: Any ideas on what I need to do to fix this? python count how many times a word appears in a list
asked Mar 30 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 3 views
3 views
Problem: Can somebody help me with this “how to reverse each word of a string in java “ ?
asked 3 days ago Raj Tyagi 15.6k points
0 votes
1 answer 2 views
2 views
Problem: Any solution to this iisue > How many objects will be created in this case string str="a"+"b"+"c" in java
asked Apr 30 ummeshani 10.8k points
2 votes
1 answer 29 views
29 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, 2020 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
Problem: How many characters can a Java String have?
asked Apr 18 sumaiya simi 43.9k points
0 votes
1 answer 6 views
6 views
Problem: Can someone please give me a hand tofind the answer of how many characters can a string hold?
asked Apr 5 ashik 14.6k points
0 votes
1 answer 2 views
2 views
Problem: How to solve the following problem:how to change letters in a string python
asked Apr 13 ashik 14.6k points