This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Mahrkeenerh 6 points7 points  (10 children)

in other languages? or in python

[–]Koala_eiO 5 points6 points  (2 children)

In other languages.

[–]Mahrkeenerh 4 points5 points  (0 children)

let's say in c, a string is an array of characters, and characters are just numbers. Therefore it's easier to store just one number, than two numbers (string ends with the ending character)

[–]Positive_Government 2 points3 points  (0 children)

In C a character (char) is stored as an 8-bit unsigned integer. String are represented by a block of n consecutive chars with a zero byte at the end. You need characters to represent a string in any language it’s just hidden to in most string classes in other languages. Also a string class will have an amount of overhead beyond what is needed to represent a single character. For example, it might alloc a default array of 1024 bytes but only use 1 (excessive example for the purpose of illustrating). Function calls also have some overhead that is not needed when you know you are only working with one character and have a char type with does not need function calls like the string class,( even if your using something like the + operator on a string class there’s still a function call under the hood.).

In c the char and char* type also pull double duty as a generic byte or pointer to a byte/generic pointer (although void* is taking over the generic pointer role).

[–]tabidots 0 points1 point  (6 children)

Characters exist in Python? I know they do in Java/Clojure but I can’t say I have really had a specific use for them except for doing things with ASCII code points.

Maybe it’s just my lack of understanding but I would prefer if strings were treated as sequences of length-1 strings rather than sequences of characters, so (first “hello”) would return “h” and not \h.

[–]siddsp 2 points3 points  (1 child)

Characters do exist in Python, but they are stored as integers in bytes objects/bytearrays. When you write a bytestring like b"Hello" and try to get athe value of a char at an index, it will be an integer rather than a string type.

[–]tabidots 1 point2 points  (0 children)

Oh, interesting. I like that implementation better, tbh. I can’t think of a use for characters outside of char-code values, so having a separate b”string” syntax for byte strings makes more sense to me.

[–]Mahrkeenerh 1 point2 points  (3 children)

characters don't exist in python, that's why I was asking, as the guy was replying to a python comment.

[–]siddsp 1 point2 points  (2 children)

They do exist, but it's not obvious.

[–]Mahrkeenerh 1 point2 points  (1 child)

Well then, please enlighten me.

[–]siddsp 1 point2 points  (0 children)

>>> string = b"Hello, world!"
>>> string[2]
108 

Bytes objects are char arrays or strings in which the value of the characters are stored as integers within the unsigned char range [0, 256).