This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

preface

In our operation development process, we often encounter all kinds of special string operations, such as escape characters, operators, operators and so on, which is more troublesome to query. Therefore, according to the special symbols often used in the work and the symbols commonly used in the summary network, the author carries on the summary to share with you.

Python escapes characters

Python escapes characters with backslashes () when special characters are needed in characters. The following table:

Escape character describe
\ Line continuation (at the end of a line
\ \ Backslash notation
Single quotes
Double quotation marks
\a Ring the bell
\b Backspace (Backspace)
\e escape
\ 000 empty
\n A newline
\v Vertical TAB character
\t Horizontal tabs
\r enter
\f Change the page
\oyy Octal numbers, yy for characters such as: \o12 for newline
\xyy A hexadecimal number, yy represents a character, for example, \x0a represents a newline
\other Other characters are printed in normal format

Python string operators

The following table instance variable a is the string “Hello” and variable B is the string “Python” :

The operator describe The instance
+ String conjunction A + b: HelloPython
* Repeat output string A *2 Output: HelloHello
[] Gets characters in a string by index A [1] output result E
[:] Intercepts a portion of a string A [1:4] output result ELL
in Member operator – Returns True if the string contains the given character ‘H’ in a outputs result 1
not in Member operator – Returns True if the string does not contain the given character ‘M’ not in a Result 1
r/R Raw strings – Raw strings: All strings are used literally, without escaping special or printable characters. Raw strings have almost exactly the same syntax as regular strings, except that the first quotation mark of the string is preceded by the letter R (which can be uppercase and lowercase). print( r’\n’ )print( R’\n’ )
% Format string

Python String Formatting

Python supports output of formatted strings. Although this can involve very complex expressions, the most basic use is to insert a value into a string with the string formatter %s. String formatting in Python uses the same syntax as the sprintf function in C.

Python string formatting notation:

Operator, describe
%c   Format characters and their ASCII codes
%s Formatted string
%d   Formatted integer
%u Format an unsigned integer
%o Format an unsigned octal number
%x Format an unsigned hexadecimal number
%X Formatted unsigned hexadecimal number (uppercase)
%f Format a floating-point number that specifies the precision after the decimal point
%e Format floating point numbers with scientific notation
%E Same as %e, uses scientific notation to format floating-point numbers
%g Short for %f and %e
%G Short for %f and %E
%p Format the address of a variable with a hexadecimal number

Formatting operator auxiliary instructions:

symbol function
* Define width or decimal precision
Use it for left alignment
+ Display a plus sign (+) before a positive number
<sp> Display Spaces before positive numbers
# Display zero before octal numbers (‘0’) and either ‘0x’ or ‘0x’ before hexadecimal numbers (depending on whether ‘x’ or ‘x’ is used)
0 The number displayed is preceded by a ‘0’ instead of the default space
% ‘%%’ prints a single ‘%’
(var) Mapping variables (dictionary parameters)
m.n. M is the minimum total width to display and n is the number of decimal places (if available)

Python3 string operators and their related, this article is based on the author’s experience and a list of commonly used special symbol information on the web. Thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.