• Register
1 vote


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)

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


“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:


Hope you understand the thing and it helps you.

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 55 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 25 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
0 votes
1 answer 13 views
I require to remove all the special characters, puntuation and and speces from a string?
asked Sep 14, 2020 Daniel Anderson 4k points
0 votes
1 answer 28 views
Problem: Hello kodlogs, Recently while attempting, the problem in the python there is a error that came can you please help sort out this problem, How can I get the first character from the first string in Python?,It seems that I could use list[0][1:] but that does not give me the first character of the string that I am looking for. mysrt = "sdf" >>> mystr[0][1:]
asked Jun 6, 2020 Gavin 15.3k points
1 vote
2 answers 38 views
Problem: I am very noob at programming language so I am doing my pracice on python. I tried to to replace multiple characters in a string in python. So I write this code: tes_str = "abababababababba" print("The main string is : " + str(tes_str)) res = tes_str('a', '%temp%')('b', ... = tes_str('a', '%temp%')('b', 'a')('%temp%', 'b') TypeError: 'str' object is not callable Can anyone help me out?
asked Apr 6, 2020 LizzyM 6.1k points
2 votes
1 answer 40 views
Problem: Well, let’s assume I have a string in python and I am willing to remove all the duplicates characters from my string. Suppose my string is “Australia” the program should return “Austrli” So, my question is simple. How can I remove duplicate characters in a string python?
asked Apr 5, 2020 Gavin 15.3k points
1 vote
1 answer 24 views
Problem: Hello sir, I want to know that how can we add a character to the string it a very easy question still, I am having some confusion in this like while printing in another language like java if I print it to output in the print function it gives the output correctly but in this case, is coming syntax error.
asked Jun 3, 2020 Gavin 15.3k points
1 vote
1 answer 16 views
Problem: Hello, good people of Kodlogs, I am a new student of Python programming language. The online course I am taking they are teaching me well. Every weekend they provide me with some problems to solve. This week, I have got the problem find all substrings of a string python. I don’t have any idea how to do this! I would appreciate your effort if you pull me up. Many thanks
asked Apr 5, 2020 Gavin 15.3k points
2 votes
1 answer 23 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
1 vote
1 answer 20 views
Problem: For the following string I tried to convert is’s first character to a upper case. So I write a program. The string: forgodsake The program I wrote is: main_str = "forgodsake" print("The main string youput is : " + str(main_str)) key = main_str[0] + ... I am getting is not right. What is the problem in my code, why am I not getting the expected result. Can anyone sort this out?
asked Jun 10, 2020 samhaz 5k points