Hướng dẫn special literals in python

String literals are described by the following lexical definitions:

stringliteral:   shortstring | longstring
shortstring:     "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring:      "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
shortstringitem: shortstringchar | escapeseq
longstringitem:  longstringchar | escapeseq
shortstringchar: 
longstringchar:  
escapeseq:       "\" 

In plain English: String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter `r' or `R'; such strings are called raw strings and use different rules for backslash escape sequences.

In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. (A ``quote'' is the character used to open the string, i.e. either ' or ".)

Unless an `r' or `R' prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are:

Escape Sequence Meaning 
\newline Ignored
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo ASCII character with octal value ooo
\xhh... ASCII character with hex value hh...

In strict compatibility with Standard C, up to three octal digits are accepted, but an unlimited number of hex digits is taken to be part of the hex escape (and then the lower 8 bits of the resulting hex number are used in 8-bit implementations).

Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the string. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.)

When an `r' or `R' prefix is present, backslashes are still used to quote the following character, but all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase `n'. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a value string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.


See About this document... for information on suggesting changes.

Hướng dẫn special literals in python

Hướng dẫn dùng np around python

Trong bài viết trước tôi đã giới thiệu cho bạn về NumPy, tìm hiểu về Mảng trong NumPy. Trong bài viết này chúng ta sẽ tiếp tục tìm hiểu về các kiểu dữ ...

Hướng dẫn dùng install mysqldb python

Trong bài này, chúng ta sẽ tìm hiểu về cách kết nối từ xa tới cơ sở dữ liệu MySQL bằng cách sử dụng Python. Việc lưu trữ dữ liệu trên một server để ...

Hướng dẫn dùng react flask python

Ở bài viết trước mình đã hướng dẫn xong thiết lập cho cả front và server(tại đây). Để chuẩn bị tiếp cho các phần hay ho như xử lý authentication, ...

Hướng dẫn dùng truncated define python

Python trunc () là một hàm toán học có sẵn hoạt động giống như một tầng () khi đầu vào đã cho là dương, mặt khác, nó hoạt động giống như ceil () khi đầu ...

Hướng dẫn dùng define pythons python

Hướng dẫn sử dụng def trong python. Bạn sẽ học được cách sử dụng def để khai báo hàm trong python, cũng như cách gọi hàm trong python đã được khai báo sau ...

Hướng dẫn bảng mã python

Cuốn sách Lập trình Python này được O2 Education sưu tầm và chia sẻ lại, chứ không phải tài liệu do chúng tôi viết. Lưu ý rằng phiên bản của cuốn sách này ...

Hướng dẫn dùng define comprehend python

Nội dung chínhList comprehension trong Python là gìSử dụng list comprehension để tạo list trong PythonCú pháp list comprehension trong pythonVí dụ cụ thể về sử dụng list ...

Hướng dẫn uninstall python mac

If you installed it using the PKG installer, you can do:pkgutil --pkgs or better:pkgutil --pkgs | grep org.python.Python which will output something ...

Hướng dẫn dùng scipy.stats python

Thư viện SciPy trong Python là gì?Là phần mềm nguồn mở miễn phí của Python cho toán học, khoa học và kỹ thuật . Thư viện SciPy được xây dựng dựa trên thư ...

Hướng dẫn python read ansi file

MS Notepad gives the user a choice of 4 encodings, expressed in clumsy confusing terminology:Unicode is UTF-16, written little-endian. Unicode big endian is UTF-16, written big-endian. In both ...

Hướng dẫn dùng normal dist python

In this Python tutorial, we will learn about the “Python Scipy Stats Norm” to calculate the different types of normal distribution and how to plot it and cover the following topics.Nội dung ...

Hướng dẫn python recursion for dummies

If you’re familiar with functions in Python, then you know that it’s quite common for one function to call another. In Python, it’s also possible for a function to call itself! A function that ...

Hướng dẫn dùng pip uninstall python

Python không chỉ là một ngôn ngữ lập trình cho người mới bắt đầu — nó là một ngôn ngữ mạnh mẽ và hoàn chỉnh, với một số lượng lớn các gói và thư ...

Hướng dẫn dùng c++ isnumber python

Hàm islower() trong Python Hàm isspace() trong Python Hàm isnumeric() trong Python trả về true nếu một chuỗi dạng Unicode chỉ chứa các ký tự số, nếu không là false.Ghi ...

Hướng dẫn dùng np.argmax python

Theo np.argmaxmặc định, hàm hoạt động dọc theo mảng phẳng , trừ khi bạn chỉ định một trục. Để xem điều gì đang xảy ra, bạn có thể sử dụng flattenmột ...

Hướng dẫn dùng pandas to_dict python

Pandas là một thư viện Python cung cấp các cấu trúc dữ liệu nhanh, mạnh mẽ, linh hoạt và mang hàm ý. Tên thư viện được bắt nguồn từ panel data (bảng dữ ...

Hướng dẫn dùng pseudo randomly python

26. Random một số trong pythonRandom và “secrets” module, bộ tạo số giả ngẫu nhiên mặc định của random module được thiết kế với trọng tâm vào mô phỏng ...

Hướng dẫn stacked bar chart python

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleIn this article, we will learn how to Create a stacked bar plot in Matplotlib. Let’s discuss some ...

Hướng dẫn python escape xml

Xin chào các bạn. Hôm nay mình sẽ giới thiệu với các bạn về việc Xử lý file JSOn và file XML trong PythonNội dung chínhXử lý file JSONLoad file từ InternetParsing ...

Hướng dẫn dùng translate translate python

Trong bài học này chúng ta sẽ tìm hiểu googletrans, hay còn gọi là Python Google Translator, đây là một module dùng để dịch văn bản qua các ngôn ngữ bằng cách sử ...

Hướng dẫn thời gian trong python

Mục lục Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi ...

Ma trận nghịch đảo trong python

Với những người đang học và làm về Machine Learning, có lẽ việc phải thao tác với ma trận là không thể tránh khỏi khi phải xử lý với những bộ dữ liệu ...

Hướng dẫn dùng re split python

Nội dung chính2. Regex là gì?2. Regex trong Python.3. Sử dụng Regex với search(), match(), split()3.1 re.match()3.2 re.search()3.3 Một số flag hay dùng trong Regular Expression4. ...

Hướng dẫn python sort list

Hàm List sort() trong Python sắp xếp các đối tượng của list. Nó sắp xếp các mục theo thứ tự giảm dần và tăng dần. Nó nhận một tham số tùy chọn reverse ...

Hướng dẫn hmac sha256 python

Python n choose k listIm attempting to generate all n choose k combinations of a list (not checking for uniqueness) recursively by following the strategy of either include or not include an element ...

Python n choose k list

Im attempting to generate all n choose k combinations of a list (not checking for uniqueness) recursively by following the strategy of either include or not include an element for each recursive ...

Hướng dẫn dùng train.a python

Dẫn nhậpTrong bài trước, chúng ta đã tìm hiểu về TỔNG KẾT LINEAR REGRESSION.Nội dung chínhDẫn nhậpDebug thuật toán Machine LearningPhân biệt các trường hợp Bias ...

Convert vietnamese to english python

Here is a short function which strips the diacritics, but keeps the non-latin characters. Most cases (e.g., à -> a) are handled by unicodedata (standard library), but several (e.g., æ ...

Hướng dẫn sech python

Nội dung chính Nội dung chính Các hàm Regex Xây dựng biểu thức chính quy Meta-Characters Ký tự đặc biệt Hàm findall() Đối tượng Match (kết quả khớp) Các ...

Hướng dẫn asin python

Hàm asin() trong Python trả về arcsin của x, giá trị radian.Cú phápCú pháp của hàm asin() trong Python:Ghi chú: Hàm này không có thể truy cập trực tiếp, vì thế chúng ...

What does pop () mean in python?

List pop in Python is a pre-defined, in-built function that removes an item at the specified index from the list. You can also use pop in Python without mentioning the index value. In such cases, the ...

Hướng dẫn dùng mate definition python

Vietnamese (Tiếng Việt) translation by Dai Phong (you can also view the original English article) Khi viết các chương trình trong cuộc sống hằng ngày, chúng ta thường gặp ...

Hướng dẫn dùng 3 pandas python

Trụ sở chính:Văn phòng: Số 27-3RD, Sunrise D, The Manor Central Park, đường Nguyễn Xiển, phường Đại Kim, quận Hoàng Mai, TP. Hà Nội.Liên hệ truyền thông: ...

Hướng dẫn dùng np.array append python

Hàm numpy.append () có sẵn trong gói NumPy. Như tên cho thấy, append có nghĩa là thêm một cái gì đó. Hàm numpy.append () được sử dụng để thêm hoặc nối các giá ...

Hướng dẫn throw exception python

Until now error messages haven’t been more than mentioned, but if you have tried out the examples you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax ...

Hướng dẫn dùng dictiona python

Mục lục Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi ...

Import python module in matlab

Access Python Modules from MATLAB - Getting StartedYou can access all standard Python® library content from MATLAB®. Likewise, you can use functionality in third-party or user-created modules. To ...

Hướng dẫn dùng shape of python

NumpyNumpy là một thư viện lõi phục vụ cho khoa học máy tính của Python, hỗ trợ cho việc tính toán các mảng nhiều chiều, có kích thước lớn với các hàm đã ...

Hướng dẫn dùng reverse dicitonary python

Một cái nhìn tổng quan về ba cách chính để đảo ngược một string trong Python, bao gồm cả các vấn đề về hiệu năng.Nội dung chínhOption 1: Reversing a Python ...

Hướng dẫn dùng two sets python

Bài viết được sự cho phép của tác giả Kien Dang ChungVideo trong bài viếtPython khác với các ngôn ngữ lập trình khác, nó đưa ra rất nhiều các cấu trúc dữ ...