Symbols in Python

by Alex
Symbols in Python

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.

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
To change a character, it must be reassigned to a variable.

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: 126
  • ord('¾') 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
Any non-negative integer up to 1114111 can be passed as an argument. Otherwise, a ValueError exception is thrown.
Essentially ord() and chr() are two reciprocal functions that are most often used together.

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
{0} Zero character
Apostrophe
Double quote
\ Backslash

Shielded characters are also called escape sequences and can be used, for example, to format strings

s = "Hello,n"Sergei""
print(s) #Will print:

Hi,
"Sergei."

Screen suppression

Sometimes a programmer needs a backslash to prevent elements from turning into escape sequences, for example when working with file paths. To do this, you should use two backslashes at once: “C:\Users\Public”. When a programmer wants to use a backslash for output but does not suppress the escape, an exception is even thrown in some cases. In the following example, there is a syntax error because “U” starts with a 32-bit Unicode character (with “u” it is 16-bit). It should be followed by a sequence of 8 digits, so there is an error:

s = "C:UsersPublic"

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in 
position 2-3: truncated UXXXXXXXXXX escape

To avoid this you use the escaping suppression method:

s = r "C:UsersPublic"
print(s)

C:UsersPublic

You can suppress escaping by using “r” before the beginning of the string (before the quotes). In fact, the interpreter, seeing an “r” before the string, automatically duplicates every backslash character. If we use this interactively, we see:

>>> s = r "C:UsersPublic"
>>> s
'C:\Users\Public'
The line in which the escape is suppressed cannot end with a backslash character. Otherwise the interpreter will raise a SyntaxError exception.

There are several ways to solve this problem:

  • Duplicate escape characters manually.
  • Use concatenation: s = r "Hello,nmir" + "\".

Related Posts

LEAVE A COMMENT