By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,547 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,547 IT Pros & Developers. It's quick & easy.

2 problems

P: n/a
Hello,Im using Python 2.4.2 and I'm starting a few very basic
programs,but theres two problems I've not found the answers for.
My first problem is I need code that will count the number of letters
in a string and return that number to a variable.
My second problem stems from the first as I need a function that can
slice the string into letters and have those put in seperate
strings.Ive tried using the normal [:1],[:2],etc but I need a function
that can do that a variable amount of times due to the varying number
of letters in the input.

This would be of great help,as python is new to me.

Jul 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

sy*********@gmail.com wrote:
Hello,Im using Python 2.4.2 and I'm starting a few very basic
programs,but theres two problems I've not found the answers for.
My first problem is I need code that will count the number of letters
in a string and return that number to a variable.
>>s = "hello"
len(s)
5
My second problem stems from the first as I need a function that can
slice the string into letters and have those put in seperate
strings.Ive tried using the normal [:1],[:2],etc but I need a function
that can do that a variable amount of times due to the varying number
of letters in the input.
>>s = "hello"
list(s)
['h', 'e', 'l', 'l', 'o']
>
This would be of great help,as python is new to me.
Welcome aboard!

Jul 7 '06 #2

P: n/a
sy*********@gmail.com wrote:
Hello,Im using Python 2.4.2 and I'm starting a few very basic
programs,but theres two problems I've not found the answers for.
My first problem is I need code that will count the number of letters
in a string and return that number to a variable.
Do you mean like this
>>s = 'abracadabra'
s.count('a')
5
>>s.count('ab')
2
My second problem stems from the first as I need a function that can
slice the string into letters and have those put in seperate
strings.Ive tried using the normal [:1],[:2],etc but I need a function
that can do that a variable amount of times due to the varying number
of letters in the input.
I'm sorry, but I don't know what you mean here.
>
This would be of great help,as python is new to me.
It's a great language!

Peace,
~Simon

Jul 8 '06 #3

P: n/a

Mike Kent wrote:
sy*********@gmail.com wrote:
Hello,Im using Python 2.4.2 and I'm starting a few very basic
programs,but theres two problems I've not found the answers for.
My first problem is I need code that will count the number of letters
in a string and return that number to a variable.
>s = "hello"
len(s)
5
My second problem stems from the first as I need a function that can
slice the string into letters and have those put in seperate
strings.Ive tried using the normal [:1],[:2],etc but I need a function
that can do that a variable amount of times due to the varying number
of letters in the input.
>s = "hello"
list(s)
['h', 'e', 'l', 'l', 'o']

This would be of great help,as python is new to me.

Welcome aboard!
Thank you greatly,this is exactly what I needed,and this language is
alot more powerful than I thought!

Jul 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.