In the collection from the previous section , We get it Python Knowledge of numerical types in data types , In this section we will learn more about Python On the data type of string and boolean type related knowledge .
1. String type
A string is a sequence of consecutive characters , It can be a collection of all the characters that a computer can represent . stay Python in , Strings belong to immutable series , Single quotes are usually used ‘ ’ Double quotes “ ” Or three quotes “‘ ’” perhaps ““ ”” Cover up . There is no semantic difference between these forms , It's just that there are some differences in form . The single quotation mark 、 The sequence of strings in double quotation marks must be on the same line , The sequence of characters in three quotation marks can be distributed on consecutive lines .
title=' I like the famous aphorism ' # Use single quotes , String content must be on one line mot_cn=" It is not the wine of despair that commands us , It's opportunity " # Use double quotes , String content must be on the same line # Use three quotes , String content can be distributed over multiple lines . mot_en="'Our destiny offers not the cup of despair, but the chance of opportunity. '"
notes : The quotation marks used at the beginning and end of a string must be the same , In addition, when you need to represent a complex string , You can also nest quotes .
stay Python Strings in also support escape characters . The so-called escape character refers to the use of The backslash \ Escape some special characters .
Escape character | explain |
\ | Line continuation operator |
\n | A newline |
\0 | empty |
\t | Horizontal tabs , Used to jump horizontally to the next tab stop |
\" | Double quotes |
\' | Single quotation marks |
\\ | A backslash |
\f | Change the page |
\0dd | Octal number ,dd For the character |
\xhh | Hexadecimal number ,hh For the character |
notes : Prefix the string delimiter with the letter r perhaps R Then the string will be output as is , The escape character will not be used escape .
2. Boolean type
Boolean type is mainly used to represent true and false , stay Python in , identifier true and false Is interpreted as a Boolean value , in addition Python Boolean values in can be converted to numeric values , among true Express 1,false Express 0.
notes :Python Values of boolean type in can be numerically operated , for example :false+1=1, But numerical operations on Boolean values are not recommended .
stay Python in , All objects can be tested for truth , among , Only the cases listed below give false values , Other objects are in if perhaps while All the sentences are expressed as true.
1.false perhaps none
2. The value is zero , Buckle 0 、0.0 、 imaginary number 0
3. Empty sequence , Package string 、 An empty tuple 、 An empty list 、 An empty dictionary
4. Instances of custom objects , The object ___bool___ Method returns false perhaps __len__ Method returns 0.
trivia :
This period of Python The string and Boolean types in the data type of are shared here , We will continue to share in the next issue Python Data type conversion in , Interested friends can pay attention to me .
At the same time, I can also focus on my personal Wechat subscription number , The articles in the garden will be pushed and updated in the subscription number for the first time .