1

I'm very new to python and I'm having trouble with a specific question. I need to count a character or word that the user will input in python. So essentially I have to count whatever the user inputs into 'Please enter a string and please enter a substring'.I added the "h = h.lower" because we have to ensure the string is al lower case. What I have so far is:

def highlight(): 
        h = h.lower()
        print (raw_input("Please enter a string: "))
        print (raw_input("Please enter a substring: "))
        print("There were",  "occurrences of" +str(raw_input)) 
1
  • 2
    lower() is used on the strings themselves. You need to assign your inputs to variables, i.e. h = raw_input('Please enter a string: ') Commented Jan 17, 2014 at 15:12

1 Answer 1

1

Your question is not clear enough, if you are talking about counting a particular character in a string then use,

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

word = 'elephant' 
word.count('e') 
#Gives you 2
Sign up to request clarification or add additional context in comments.

2 Comments

Im trying to count a specific character. I get that character from whatever someone inputs in python. So i run the code in python and type in a sentence after "Please enter a string". Lets say they type in 'elephant', I want to find all the e's in that word.
>>> word = 'elephant' >>> word.count('e') >>> 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.