String encoding format
What is the coding format?
In layman's terms, a coding format is a rule for coding
Specifying the encoding format at the beginning of the script tells the operating system what encoding rules to use to parse the code.
Common Encoding Formats
gbk
Chinese encoding format
ascii English encoding format, this encoding format does not support parsing Chinese:
utf-8 is an internationally recognized encoding format.urgeUse this encoding format!
String Formatting
What is string formatting?
A fixed string with some elements that change according to the value of a variable.
For example, a string'Today is xx, xx, week x, xxxx'
The xxx's are variables that we can use in strings in the same way we format strings
Scenarios for formatting strings
For example, when sending mass text messages and mass emails, the content sent is the same, and the sender is the variable
First formatting method-%
This is accomplished using the operator %, where the left side is a string with formatting characters (there can be more than one) in the middle, and the right side is a tuple with elements that are variables replacing the formatting characters on the left side.
The specific use is as follows:
str = 'my name is %s,my age is %s' % ('xiaoming', 20)
If there is only one % on the left side, the right side does not need to be wrapped in ():
str = 'my name is %s' % 'xiaoming'
Attention:Formatting symbols on the left with variables replacing formatting symbols on the rightThe number must be the same on both sides
Example:
The second formatting method - format function format()
Used to format strings and is more flexible to use than %.
String body use with format{}
Instead of formatting characters, the{}
The order can be specified in the
str = (data, data, data...)
Example:
The order of replacement is specified in {}:
Attention:As with %, the formatted quantity and the replacement variable, the quantities on both sides need to be the same
Third formatting method - f-strings (supported by python 3.6 and later)
Usage:
name = 'xiaoming' # Define the variables first str_1 = f'How are you?{name}'
The second recommended formatting method is the format() function.
Detailed explanation of formatting for different data types
%
- %c
The %c variable only supports integers or a single character, otherwise an error will be reported.
- %u %f %d
The latter variable types should be passed in according to the rules, otherwise an error will be reported
The variable after %d is a floating-point type and will not report an error, but will become an integer type.
The variable after %f is an integer, which will not report an error, but will become a floating-point type
- %s is a generic string followed by all data types
format()
Formatting for different data types is also supported, but some formatting characters, such as u, c
Uncommon formats
String escape characters
What are escape characters?The function of converting a character to another meaning, this kind of character is called an escape character
Escape characters in python
Example:
Invalidating escape characters
print(r'String with escaped characters') # The escape character is not valid at this point
to this summary of common operations in python string (two) of the article is introduced to this, more related python string content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!