strlen() (C++) or similar?


I know that this is not really Panda3D-related, but may be somebody can give me a short help/hint…

I want to determine a strings length because I want to search the string for a particular character… I want to do it the way that I want to determine the strings complete length and then just let python search each character of the list(string) from 0 to strlen(string) and compare if the character searched is present.

My question is: is there a operation similar ot strlen() from C++ or a more secure one integrated into python that can tell me the string size? What do I need to import? Due to the fact that this function will be called later very often it would be good to have a simple solution for that… In case I can do it without the need to get the exact strings size, I would be glad too, but I do not know if the character will be contained and I can’t use a search for another character that I have designed to be hit first to show the end of the string (because I can’t be sure that the string will be transmitted correctly)

As you see, even I have very noobish questions :smiley:

Regards, Bigfoot29

a string has a length just like a list:


this returns 9

you can also iterate directly over a string:

i = 0
s = "Numbers"
for char in s:
    print i, char
    i += 1

this would print:
0 N
1 u
2 m
3 b
4 e
5 r
6 s

Ah, great russ… I thought I need to do a list of the string first :slight_smile:

This is going to save a lot of processing time later on :slight_smile:

as for len: thank you (again) - didn’t know that its that simple :smiley:

Regards, Bigfoot29

So here is the stuff I needed the help for:


        if received[0]=='!':
      ,length_received,' ')
                print "space at location: ",spaceAtLocation
    def compare(self, text='text', length=1, compareValue=' '):
        while i<length:
                if text[i]==compareValue:


What does nothing else than just search for a space to separate a command from variables that can be given from within a console/chat client/whatever and returns the position of the space. With that knowledge you can now sort out the command and handle the command separate from the arguments.

Regards, Bigfoot29

you might want to look at string.split:

import string

text="print 5"

(command,data)=string.split(text," ")

Thanks… that is working - as long as you don’t have more than 1 space in your string :slight_smile: because IF you have that, you will get “ValueError: unpack list of wrong size” :smiley:

Regards, Bigfoot29

Edit: A simple way to avoid it:

import string

text='test me'
for c in text:
    if c==' ':
if i==1:
    (command, data)=string.split(text, ' ')
    print command
    print data

But as said… thanks for your help :slight_smile: I guess the way it is now is the most elegant one… g

well there’s actually two options on the python side.

If you only want the first bit of data after the command:

import string 

text="print 5 garbage" 

(command,data)=string.split(text," ")[0:2] 

but what if you want multi word data (ie “register Paul Newmann”),
split has an extra variable for split count.

import string 

text="register Paul Newmann" 

(command,data)=string.split(text," ",1)

Additionally, you could simply do

text = "createnew object color=blue"
args = text.split(" ")

#This would give you a list called args that contains each word
#as a seperate entry.

You’ll have to choose to either seperate each word into it’s own entry, or to seperate only the first one out. Basically, if you anticipate that most commands are going to take a single string argument (like /tell or /msg), go with zpavlov’s version, as you’ll end up doing extra commands less often. If, on the other hand, you find that most often commands are called with multiple single-world arguments, it would probably be more efficient to use the above method.

As for the length question, just to add some info:

len() is a built-in function that pretty much just calls len() on whatever object you pass it, so if you have a custom class that you want to get the length of (in a manner that most python people are familiar with), you can just implement the len method:

class MyListWrapper:
  def __init__(self):
    self.myList = [1, 2, 3, 4]

  def __len__(self):
    return len(self.myList)

mlw = MyListWrapper()

len(mlw) # 4

Obviously that example is silly because you could just derive a class from List and not need to reimplement the len func, but it shows the concept. This pattern applies to most every built-in function, you can write str, repr, cmp, add, and just about every other mathematical or logical operation you can use in the language.