how to count how many of a certain character in a string?

This is the place for queries that don't fit in any of the other categories.

how to count how many of a certain character in a string?

Postby stefaaaan » Mon Sep 16, 2013 8:33 pm

Hi,

I'm totally new to Python but i have to make an exercise for school, but i couldn't find out how to write this in python. I think it's really easy.
i have to make a function called 'countsymbol that counts a singlecharacter string as 1st paramater and a string as the 2nd parameter and returns how many times the 1st string is found in the 2nd string

Example of the answer:
Code: Select all
countsymbol(’@’, ’abc’)

Result
Code: Select all
0

Code: Select all
countsymbol(’a’, ’abc’)

Result
Code: Select all
1

Code: Select all
countsymbol(’b’, ’abc’)

Result
Code: Select all
1


Can somebody help please :) THANKS
Last edited by Yoriz on Mon Sep 16, 2013 8:49 pm, edited 2 times in total.
Reason: First post lock, Added code tags, Changed the title
stefaaaan
 
Posts: 4
Joined: Mon Sep 16, 2013 8:28 pm

Re: really really simple questions?

Postby Yoriz » Mon Sep 16, 2013 8:42 pm

Hi, welcome to the forum.
Please read the new user read this link in my signature.

What have they shown you how to do so far?.
What method do they want you to use to solve the problem?.
What have you tried so far to solve the problem?.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: really really simple questions?

Postby stefaaaan » Mon Sep 16, 2013 8:48 pm

Yoriz wrote:Hi, welcome to the forum.
Please read the new user read this link in my signature.


i think i should be something like this:
def countsymbol(input):
words = ??
table = ??
for w in words:
?
return table

this is really new for me so maybe i understand it when seeing the solution?
Last edited by Yoriz on Mon Sep 16, 2013 11:23 pm, edited 2 times in total.
Reason: Corrected quote
stefaaaan
 
Posts: 4
Joined: Mon Sep 16, 2013 8:28 pm

Re: how to count how many of a certain character in a string

Postby Yoriz » Mon Sep 16, 2013 8:56 pm

Hi, again
when you post code you should use code tags as it says in the link i asked you to read please.
Your function will need to recieve 2 arguments.
it will then create a variable to count how many of the first argument it has seen.
then it loops through the second argument and adds to the count variable each time first argument equals the looped item.
then return the count.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: how to count how many of a certain character in a string

Postby stefaaaan » Mon Sep 16, 2013 9:00 pm

Yoriz wrote:Hi, again
when you post code you should use code tags as it says in the link i asked you to read please.
Your function will need to recieve 2 arguments.
it will then create a variable to count how many of the first argument it has seen.
then it loops through the second argument and adds to the count variable each time first argument equals the looped item.
then return the count.



Thanks, and how will the code look like? Can you write this function in Python or are you not familiar with Python?
stefaaaan
 
Posts: 4
Joined: Mon Sep 16, 2013 8:28 pm

Re: how to count how many of a certain character in a string

Postby Yoriz » Mon Sep 16, 2013 9:08 pm

stefaaaan wrote:Thanks, and how will the code look like? Can you write this function in Python or are you not familiar with Python?

:lol: I wanted to see how you would right it in python.
if i just give you the answer, i will have done your homework for you.
i've given you a clue to how the code will be structured.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: how to count how many of a certain character in a string

Postby stefaaaan » Tue Sep 17, 2013 8:29 am

But can you help me a bit?


Code: Select all
def countsymbol(symbol, word):
word = 'this is a sentence'
symbol = 'sentence'

for w in words:
if ? then:
count = count + 1
return count


Somehting like that?
stefaaaan
 
Posts: 4
Joined: Mon Sep 16, 2013 8:28 pm

Re: how to count how many of a certain character in a string

Postby stranac » Tue Sep 17, 2013 9:32 am

Not quite.
In python, indentation is a big deal.
Your code will not work unless properly indented.

There is no need for you to define word and symbol iside the function; those will be passed as arguments.

Also, you're looking for:
Code: Select all
if w == symbol:
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: 7stud and 4 guests