Python (4) raw, multiline, Unicode string, pythonunicode
Define strings in Python
What is a string. String can be used''Or"".
If the string itself contains'What should I do? For example, we want to represent a string I'm OK In this case, you can use" "Including:
"I'm OK"
Similarly, if the string contains", We can use' 'Including:
'Learn "Python" in imooc'
If the string contains both'Include"What should I do?
At this time, some special characters of the string must be "escaped ".\Escape.
To represent a stringBob said "I'm OK".
Because 'and "May cause ambiguity, we insert\It indicates that this is a common character and does not represent the start of the string. Therefore, this string can be expressed
'Bob said \"I\'m OK\".'
Note:Escape Character \ is not included in the content of the string.
Common escape characters include:
\ N indicates line feed
\ T indicates a tab
\ Represents \ character itself
Raw string and Multiline string in Python
If a string contains many characters that need to be escaped, It is very troublesome to escape each character. To avoid this situation, we can add a prefix before the stringrIt indicates that this is a raw string, and the characters in it do not need to be escaped. For example:
r'\(~_~)/ \(~_~)/'
Howeverr'...'The notation cannot represent a multi-line string or contain'And"(Why ?)
You can use'''...'''Indicates:
'''Line 1
Line 2
Line 3'''
The representation of the above string is exactly the same as the following:
'Line 1 \ nLine 2 \ nLine 3'
You can also addrTo convert the multi-line string into a raw string:
r'''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!'''
Unicode string in Python
There is another encoding problem in the string.
Because the computer can only process numbers, if you want to process text, you must convert the text into numbers before processing. The earliest computer was designed to use eight bits as a byte. Therefore, the maximum integer represented by a word energy saving is 255 (Binary 11111111 = decimal 255 ), 0-255 is used to indicate uppercase and lowercase English letters, numbers, and symbols. This encoding table is called ASCII encoding. For example, the uppercase letter A is encoded as 65 and the lowercase letter z is encoded as 122.
To express Chinese characters, it is clear that one byte is not enough. It requires at least two bytes and cannot conflict with ASCII encoding. Therefore, China has developed GB2312 encoding to encode Chinese characters.
Similar to other languages such as Japanese and Korean. Unicode came into being to unify the encoding of all texts. Unicode unifies all languages into a set of encodings, so that there will be no garbled issues.
Unicode usually represents a character in two bytes. The original English encoding is changed from single-byte to dual-byte. You only need to set the height of all bytes to 0.
Because Python was born earlier than the Unicode standard, the earliest Python only supports ASCII encoding, and the common string 'abc' is inside Python.
Python later added support for Unicode. The Unicode string is represented by U'... ', for example:
Print u 'Chinese'
Chinese
Note:Without u, Chinese characters cannot be displayed normally.
In additionuIn addition, it is no different from a common string. The Escape Character and Multiline representation are still valid:
Escape:
U'chinese \ n Japanese \ n Korean'
Multiple rows:
U''' first line
Line 2 '''
Raw +Multiple rows:
Ur '''python Unicode strings support "Chinese ",
"Japanese ",
"Korean" and other languages '''
If the Chinese character string encounters UnicodeDecodeError In the Python environment, this is because the format of the. py file is incorrect. You can add comments in the first line.
# -*- coding: utf-8 -*-
The purpose is to tell the Python interpreter to read source code with UTF-8 encoding. Save it as... with Notepad ++ and save it in UTF-8 format.