The char type is a data type used to store single characters in different encodings. It is widely used in lower-level programming languages such as C. Even strings there are arrays consisting of char elements. In Python, there is no separate type for characters. Even if you assign a variable the value ‘a’, it will be a string type.
Table of Contents
The char alternative in Python 3
The language developers decided there was no need to allocate an entire type to individual characters. There are several reasons for this:
- A string type provides all the functionality a programmer needs to handle both strings and characters.
- Python is a dynamically typed language, splitting it into string and character types would add more problems with implementing automatic type detection.
Therefore, no matter how the programmer tries, variables will have a string type:
-
s = 'a'print(type(s))<class 'str'>
-
s = 'Hello's = s[0] # Assign the variable element 'H'print(type(s))<class 'str'>
Any character in Python is a single string, which allows you to use the same functions to work with it as you do with strings. A string is an immutable sequence, and since a character is also a string, an exception will be raised if you try to change it:
s = 'O' s[0] = 'A' # TypeError exception
Functions
Despite combining the two types, the Python 3 programming language has functions for working specifically with symbols. Each element of a string is a coded sequence of bits. In Python, characters can be represented as integer int. The built-in functions ord()
and chr()
are used to handle their normal and numeric representations.
ord()
This function returns the numeric representation of the character passed as an argument. That is, you can use it in Python to determine the code of a character (C has a similar function to convert char to int), for example:
ord('~')
result: 126ord('¾')
result: 190
The function only works if a single character is passed as an argument; a TypeError exception is thrown if you try to pass a string. It can be used to get the numeric representation of any Unicode character.
chr()
The function returns a character corresponding to its numeric representation, which is passed as an argument:
chr(126)
result: ~chr(190)
result: ¾chr(11141112)
valueError exception
Shielding
Shielded characters are special characters after the backslash “” that perform certain actions and conversions.
Shielded sequence | Function |
n | Skip to new line |
t | Tabulation |
r | Carriage return to the beginning of the line |
x | Numbers in hexadecimal representation |
o | Numbers in octal representation |