473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Easiest way to calculate number of character in string

Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal
Dec 21 '05 #1
7 2366
should = 42
has = Zeile.count(';')
if has < should:
Zeile += ";"*(should - has)

cheers, claude

Dec 21 '05 #2
On Wed, 2005-12-21 at 09:03, P. Schmidt-Volkmar wrote:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal


AnzahlSemikolon = Zeile.count(';')
if AnzahlSemikolon < 42:
Zeile += ';'*(42-AnzahlSemikolon)

HTH,

Carsten.
Dec 21 '05 #3
Il 2005-12-21, P. Schmidt-Volkmar <no*****@tauth.de> ha scritto:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?


One way can be this I think:

s = "your_string_full_of_;;;;;"
num_of_semicolons = len(s.split(";") - 1)

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Dec 21 '05 #4
P. Schmidt-Volkmar wrote:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:

How can this be achieved easily?


Is this homework? str.count() and string multiplication are your friends here. See
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/typesseq.html

Kent
Dec 21 '05 #5
"P. Schmidt-Volkmar" <no*****@tauth.de> writes:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal


What about this:

Zaehler += ';'*max(0,42-Zaehler.count(';'))

Dec 21 '05 #6
Ove Svensson <sv**********@hotmail.com> writes:
"P. Schmidt-Volkmar" <no*****@tauth.de> writes:
Hi there,

I have a string in which I want to calculate how often the character ';'
occurs. If the character does not occur 42 times, the ";" should be added so
the 42 are reached.

My solution is slow and wrong:
for Position in range (0, len(Zeile)):
if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
if AnzahlSemikolon < 42:
for Zaehler in range(AnzahlSemikolon, 42):
Zeile = Zeile + ';'
Dreckskram = Dreckskram +1

How can this be achieved easily?

Thanks,

Pascal


What about this:

Zaehler += ';'*max(0,42-Zaehler.count(';'))


Sorry, should have been

Zeile += ';'*max(0,42-Zeile.count(';'))
Dec 21 '05 #7
On 21 Dec 2005 15:57:35 +0100, Ove Svensson <sv**********@hotmail.com> wrote:
Ove Svensson <sv**********@hotmail.com> writes:
"P. Schmidt-Volkmar" <no*****@tauth.de> writes:
> Hi there,
>
> I have a string in which I want to calculate how often the character ';'
> occurs. If the character does not occur 42 times, the ";" should be added so
> the 42 are reached.
>
> My solution is slow and wrong:
> for Position in range (0, len(Zeile)):
> if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
> if AnzahlSemikolon < 42:
> for Zaehler in range(AnzahlSemikolon, 42):
> Zeile = Zeile + ';'
> Dreckskram = Dreckskram +1
>
> How can this be achieved easily?
>
> Thanks,
>
> Pascal
>
>


What about this:

Zaehler += ';'*max(0,42-Zaehler.count(';'))


Sorry, should have been

Zeile += ';'*max(0,42-Zeile.count(';'))


I think You don't need the max
for n in xrange(-3,4): print '%3s: %r'%(n, n*';')

...
-3: ''
-2: ''
-1: ''
0: ''
1: ';'
2: ';;'
3: ';;;'

I.e.,

Zeile += ';'*(42-Zeile.count(';'))

should work, since a string is a sequence type and

http://docs.python.org/lib/typesseq.html

Says
"""
Operation Result Notes
...
s * n , n * s n shallow copies of s concatenated (2)
....
(2)
Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type as s). ...
"""

I guess it might be nice to mention this in help(str) also, to publish a useful fact better.
Maybe under str.__mul__ ?

Regards,
Bengt Richter
Dec 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
13
by: Martin Herbert Dietze | last post by:
Hi, I need to calculate the physical length of text in a text input. The term "physical" means in this context, that I consider 7bit-Ascii as one-byte-per character. Other characters may be...
12
by: paii, Ron | last post by:
Sorry about that last one. Does anyone know how to calculate the width a string of text for given Font name and size? I want to buildup a block of text strings to display in a unbound control,...
6
by: sang | last post by:
Hai i am working in java development team, i want to calulate the number of string occurs in the input. that is String s = ("java","mysql"); in this java is one string and mysql is one...
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
13
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of...
39
by: Umesh | last post by:
Plese help. Is there any software by which we can do that?
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.