-3

every time a computer stores a string in memory, for example the word 'apple', it's not using 5 characters, it's using 6 characters, 'a','p','p','l', 'e' + 'null' therefore putting 00000000 aka '\0' in the last byte. so here is the code that would work in c:

// assuming string A = 'apple'
int count = 0;
while (A[count] != '\0')
{
  n++;
}

so i tried the following code in python, it did not work, what went wrong?

A = 'apple'
count = 0
while A[count] != '\0':
    count+=1

print(count)

Note: the point here is not to get the length, the point is how do i get the length using while != 00000000

Additional Note: how does python store strings in memory, does it put 00000000 in the end like c does? if it doesn't ,lets say i put a 00000000 in memory using c, then how do i find that 00000000 in memory with python?

DADI
  • 41
  • 5
  • 4
    `python` `strings` are not `NULL` `delimitered` – han solo Mar 06 '19 at 15:46
  • Python does not store strings that way. That is a C convention. – Max Mar 06 '19 at 15:47
  • 2
    You get the length of a python string with `len`, period. There is only one obvious way to do it. BTW - in C one should use `strlen` as the compiler would more readily know how to optimize it. – Antti Haapala -- Слава Україні Mar 06 '19 at 15:47
  • Python and C are two different languages, you know, with different syntax and semantics. – machine_1 Mar 06 '19 at 15:50
  • but the string is stored in memory right? or does python not store strings in memory the way c does? so where is the string, how do i find that 00000000 in memory – DADI Mar 06 '19 at 15:56
  • Python is a high level language, if you want to do things like that then use C. *"every time a computer stores a string in memory...."* - what you show is a language convention in C, it does not necessarily apply to other languages, many of which store a length rather than use a delimiter. "*a computer*" is not specifically storing a string, it only knows about bit patterns, 1s and 0s, it is up to the application to decide what they represent. – cdarke Mar 06 '19 at 16:40

1 Answers1

1

Python doesn't null-terminate strings; the metadata is stored elsewhere.

If you're looking to get the length of a string, you can use count = len(A)

Taylor Nelms
  • 384
  • 2
  • 16
  • so how do i make it work, if i have to use while != '\n' – DADI Mar 06 '19 at 15:49
  • I'm confused as to what you're looking to accomplish; can you give an example of what your input looks like, and what you'd like as your output? If you have a string you want to split up into multiple strings with a delimiter, the `split` function might be what you're looking for. (https://docs.python.org/2/library/string.html#string.split) Otherwise, if you're just looking at individual strings, they're already separated for you; one of the nice things about Python is that it handles a lot of the weirdness about strings/arrays/pointers that require careful handling in C. – Taylor Nelms Mar 06 '19 at 15:54
  • how does python store strings in memory, does it put 00000000 in the end like c does? if it doesn't ,lets say i put a 00000000 in memory using c, then how do i find that 00000000 in memory with python? – DADI Mar 06 '19 at 15:57
  • 1
    The idea of "access in memory" for Python is a bit nonsensical; by the time you hit python code, it tends to be pretty far removed from the actual memory implementation. I wouldn't hold out hope on ever being able to find that null-terminating byte (if it even exists; they might be prepending strings with length information instead) – Taylor Nelms Mar 06 '19 at 16:55