String Data Structure
stringis a data type thats represented by an array of characters.
In Python, each character follows the unicode standard, which is a standardized list of characters (including emojis).
Strings are immutable, which means they cannot be modified.
So unlike arrays, updating a position in a string is not allowed:
Immutability also implies that some Pythonic operations, such as string concatenation, actually allocates new memory to create the resulting concatenated string.
+to concatenate strings is costly, it's often better to operate on an array instead, and use join at the end.
Under the hood, Python's
joinallocates a mutable bytearray, and updates each position with the byte representation of each character, including those of the separator, before converting back into a string.
joinconcatenates strings in linear time.
Useful Python String Functions
ordto convert a unicode character into its unicode code.
ordis often useful to represent characters as integers, or access a character that is a specific distance away from another character.
Opposite of ord, use
chrto convert an integer that represents a unicode code to its unicode character.