Chapter 2 of Python Foundation

• Binary system
• Character encoding
• Basic Data Type - Number
• Basic Data Type - String
• Basic Data Type - List
• Basic data type - tuples
• Variable, immutable data types and hash
• Basic Data Type - Dictionary
• Basic Data Type - Set

Binary system

Binary system is a kind of system used in computing technology. The binary number is composed of 0 and 1 digits. Its base number is 2. The binary rule is "every two in one", which was discovered by Leibniz, a German mathematic and philosophical master in the 18th century. Current computer systems are basically binary systems, and data are mainly stored in the form of complements. The binary system in a computer is a very small switch, with "on" for 1 and "off" for 0.

Conversion of binary and decimal systems

The decimal values represented by the n-th digit of the binary system all follow the rule of the n-th power of 2.

Bucket method

First write out the values they represent at one time, and then fill in the corresponding positions according to the values in the decimal system.~~~
The conversion from decimal to binary is the same, as long as the decimal value corresponding to the bit of binary 1 is added.

```        128     64      32      16      8       4       2        1

20       0       0       0       1      0       1       0        0
200      1       1       0       0      1       0       0        0```

Character encoding

The conversion between decimal system and binary system can only solve the problem that computer understands numbers, so how to make computer understand for words?
So there is a way to save the nation by curve. Since the number can be converted into decimal system, we only need to find a way to solve the problem of converting text into digital system. Then can the text be expressed as binary system?
But how should text be converted into binary? It's mandatory conversion.
We have forcibly agreed on a table to correspond words to numbers. This table is equivalent to translating. We can use a number to compare the words in the corresponding table, and vice versa.

ASCII code

ASCII Chart

ASCII (American Standard Code for Information Interchange) is a computer coding system based on Latin letters. It is mainly used to display modern English and other Western European languages. It is the most common single-byte coding system and is equivalent to the international standard ISO/IEC 646.

Since computers were invented by Americans, only 127 letters were first coded into computers, that is, upper and lower case letters, numbers and some symbols. This coding table is called ASCII coding. For example, the coding of capital letter A is 65, and the coding of lowercase letter z is 122. The last 128 are called extended ASCII codes.

Now we know that the alphabetic symbols and the corresponding tables of numbers have existed for a long time. Then, according to some decimal systems we have now, we can convert them into binary coding strings.
For example:

```The number corresponding to a space is 0 translated into binary 0 (note that characters 0 and integer 0 are different)
A check corresponds to a number of 251 translated into binary is 11111011```

Question: If we want to print two spaces and a check, it should be [`0011111011]. But the question arises. How do we know where to get a character? The wise man came up with a solution. Since there are 255 characters in all, the longest one is 111111111 118 bits. It's better to convert all binaries into 8 bits instead of 0.

In this way, the just two spaces are written in pairs [`000000000000011111011], and the binary value of each character can be known by reading only eight characters at a time.

Here, [`each bit 0 or 1 takes up bit (bit)], which is the smallest unit of representation in a computer. Every 8 bits make up one byte, which is the smallest storage unit of the computer.

```    Bit bit, the smallest unit of representation in a computer
8 bit = 1 bytes, the smallest unit of storage, abbreviated 1bytes to 1B
1KB = 1024KB
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
......```

So, now that I have learned ASCII code, as an English programmer, it is basically satisfactory, but as a Chinese programmer, do you think there is something missing?

GBK and GB2312

Obviously, it is the most important thing for us to display Chinese in the computer, but there is not even a side capital in the ASCII table just now. So we need a table about the relationship between Chinese and numbers. As we have seen before, one byte represents 256 characters at most. Obviously, it is not enough to deal with one byte in Chinese. So we need to use two bytes to represent it, and it can not conflict with ASCII coding. Therefore, China has formulated GB2312 coding to encode Chinese.

At this time, the problem arises again: when a game or movie from abroad is introduced to China in more than 100 languages around the world, what should be done with the coding?

Unicode

Therefore, Unicode came into being. Unicode unifies all languages into a set of codes, so that there will be no scrambling problem.

Unicode standards are also evolving, but the most common use is to represent a character in two bytes (four bytes are required if very remote characters are to be used). Unicode is directly supported by modern operating systems and most programming languages.
Now, make a distinction between ASCII coding and Unicode coding:

ASCII encoding is one byte, while Unicode encoding is usually two bytes.

The letter A is encoded by ASCII as 65 decimal and 01000001 binary.

Character 0 is encoded by ASCII in decimal 48 and binary 00110000.

Chinese character "Zhong" has gone beyond the scope of ASCII encoding. Unicode encoding is decimal 20013 and binary 01001110 00101101.

You can guess that if ASCII coded A is coded in Unicode, it only needs to add 0 before it. Therefore, the Unicode code code of A is 00000000 01000001.

New problems arise again. If you change to Unicode coding, the problem of scrambling will disappear. Then the following is that if you write basically in English, Unicode coding needs twice as much space as ASCII coding, and it is not cost-effective in storage and transmission.

UTF-8

Therefore, in the spirit of saving, UTF-8 encoding that transforms Unicode encoding into "variable length encoding" has appeared. UTF-8 encoding encodes a Unicode character into 1-6 bytes according to different digital sizes. Common English letters are encoded into 1 byte. Chinese characters are usually 3 bytes. Only rare characters can be encoded into 4-6 bytes. If the text you want to transmit contains a large number of English characters, encoding with UTF-8 saves space:

```    Character ASCII Unicode UTF-8
A          01000001     00000000 01000001              01000001
Zhongx 01001110 00101101 11100 101100 10100 10100 10101101```

As shown in the figure above, UTF-8 encoding has an additional advantage: ASCII encoding can actually be seen as part of UTF-8, so a large number of legacy software that only supports ASCII encoding can continue to work under UTF-8 encoding.

This paper clarifies the area relationship between ASCII and UTF-8, Unicode and UTF-8, and summarizes the general character encoding methods in computer systems.
Unicode encoding is used uniformly in computer memory. When it needs to be saved to hard disk or transmitted, it is converted to UTF-8 encoding.

When editing with notepad, UTF-8 characters read from files are converted into Unicode characters and stored in memory. After editing, Unicode is converted into UTF-8 when saving, and then saved to files.

• windows defaults to GBK
• MacOSLinux defaults to UTF-8
• Python 2 is encoded as ASCII
• Python 3 is coded UTF-8

Basic Data Type - Number

Boolean type

bool type has only two values: True and False

The reason why bool values are classified as numbers is that we are also accustomed to using 1 for True and 0 for False.

integer

Integers in Python belong to int type and are represented by decimal system by default. In addition, binary, octal and hexadecimal systems are also supported.

Binary conversion

Although computers only know binary, python's number is still decimal by default in order to cater to our habits. Some methods are also provided to help us do the conversion, such as the conversion from binary to binary using the [`bin'method, and adding 0b to the conversion result to indicate that it is a binary number.

```>>> num = 132
>>> bin(num)
'0b10000100'```

Since decimal system can be converted to binary system, the same principle can also be used to convert to other systems. python also provides us with the method of converting decimal system to oct al system and hex system, respectively. Octal is preceded by 0o and hexadecimal by 0.

```>>> num = 129
>>> oct(num)
'0o201'
>>> hex(num)
'0x81'```

Remainder operation

```>>> 16%5
1```

divmod

```>>> divmod(16,3)
(5, 1)          #5 is quotient, 1 is remainder```

float

Floating point number is a numerical representation of a specific subset of rational number, which is used to approximate any real number in a computer. Specifically, the real number is obtained by multiplying the integer power of an integer or fixed number (i.e., the tail) by an integer power of a cardinal number (usually 2 in a computer), which is similar to the scientific counting method with a cardinal number of 10.

python's floating point number is the decimal number in mathematics (finite decimal and infinite cyclic decimal)

In operation, the result of integer and floating-point operation is also a floating-point number.

Why Floating Points

Floating-point numbers are decimal numbers. They are called floating-point numbers because they are expressed in scientific notation.
The decimal position of a floating-point number is variable, for example,
1.23x109 and 12.3x108 are equal.
Floating point numbers can be written mathematically, such as 1.23, 3.14, -9.01, and so on. However, for large or small floating-point numbers, it is necessary to use scientific counting to express 10 instead of e:
1.23*109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, etc.
Integers and floating-point numbers are stored differently in computers. Integer operations are always accurate, while floating-point operations may have rounding errors.

On the Problem of Uncertainty of Decimals

python defaults to 17-bit accuracy, which is the last 16 Decimal places. Although there are 16 Decimal places, the more backward the accuracy is, the more inaccurate it is.

First of all, this problem does not exist only in python, but also in other languages.

Secondly, decimal inaccuracy is due to infinite cycles in the process of converting to binary, and deviations occur when saving.

For example, the decimal part 0.2 of 11.2 is converted to binary system, and the infinite loop of 00110011100110011 is converted to binary system.

Single precision uses 23 bits to store the tail part (the first 9 bits to store index and symbol), and 0.6 is also infinite cycle.

The question here is, what do we do when we need higher accuracy (over 16 digits) in our calculations?

```#Here you need to use the getcontext() and Decimal() methods of the decimal module
>> a = 3.141592653513651054608317828332
>>> a
3.141592653513651
>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> getcontext().prec = 50      #Modify the tail after the decimal point to 50
>>> a = Decimal(1)/Decimal(3)   #The result is correct in the calculation of fractions. If the ultra-long precision decimal is defined directly, it will not be accurate.
>>> a
Decimal('0.33333333333333333333333333333333333333333333333333')
>>> a = 3.141592653513651054608317828332
>>> a
3.141592653513651
>>> Decimal(a)
Decimal('3.141592653513650912344701282563619315624237060546875')```

complex

complex is composed of real and imaginary numbers.

To understand complex numbers, in fact, we need to know imaginary numbers first about complex numbers. Imaginary number (that is, false and unreal number): A number whose square is complex is called an imaginary number.

Complex numbers are numbers a + b I that can be written in the following form, where a and b are real numbers and I are imaginary units (i.e. - 1 roots). In complex a + b i, A is called the real part of the complex number, b is called the imaginary part of the complex number (imaginary number refers to the number whose square is negative), and I is called the imaginary unit.

When the imaginary part is equal to zero, the complex number is real; when the imaginary part is not equal to zero, the complex number is called imaginary number.

Note, the imaginary part of the letter j can be capitalized.

Basic Data Type - String

Definition and creation of strings

A string is an ordered collection of characters used to store and represent basic text information,''or',''or''' or''''contained in memory, called a string.

Establish:

`s = 'Hello XiaoYafei!Hello Python!'`

Characters of Strings and Common Operations

Characteristic

1. Define the character set in left-to-right order. The following table is accessed in order from 0.

```    str      =           hello
Index 01234```

Supplement:
1. Both single and double quotation marks cannot cancel the meaning of special characters. If you want to cancel the special meaning of all characters in quotation marks, add r before boot, such as:
python name = r'pytho\tn'

• Use ctrl plus left mouse button to view source code
```class str(object):
"""
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
"""
def capitalize(self): # real signature unknown; restored from __doc__
"""
S.capitalize() -> str

Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
"""
return ""

def casefold(self): # real signature unknown; restored from __doc__
"""
S.casefold() -> str

Return a version of S suitable for caseless comparisons.
"""
return ""

def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.center(width[, fillchar]) -> str

Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)
"""
return ""

def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are
interpreted as in slice notation.
"""
return 0

def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
"""
S.encode(encoding='utf-8', errors='strict') -> bytes

Encode S using the codec registered for encoding. Default encoding
is 'utf-8'. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.
"""
return b""

def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.endswith(suffix[, start[, end]]) -> bool

Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
"""
return False

def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__
"""
S.expandtabs(tabsize=8) -> str

Return a copy of S where all tab characters are expanded using spaces.
If tabsize is not given, a tab size of 8 characters is assumed.
"""
return ""

def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.find(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.
"""
return 0

def format(self, *args, **kwargs): # known special case of str.format
"""
S.format(*args, **kwargs) -> str

Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
"""
pass

def format_map(self, mapping): # real signature unknown; restored from __doc__
"""
S.format_map(mapping) -> str

Return a formatted version of S, using substitutions from mapping.
The substitutions are identified by braces ('{' and '}').
"""
return ""

def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

"""
return 0

def isalnum(self): # real signature unknown; restored from __doc__
"""
S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
"""
return False

def isalpha(self): # real signature unknown; restored from __doc__
"""
S.isalpha() -> bool

Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
"""
return False

def isdecimal(self): # real signature unknown; restored from __doc__
"""
S.isdecimal() -> bool

Return True if there are only decimal characters in S,
False otherwise.
"""
return False

def isdigit(self): # real signature unknown; restored from __doc__
"""
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
"""
return False

def isidentifier(self): # real signature unknown; restored from __doc__
"""
S.isidentifier() -> bool

Return True if S is a valid identifier according
to the language definition.

Use keyword.iskeyword() to test for reserved identifiers
such as "def" and "class".
"""
return False

def islower(self): # real signature unknown; restored from __doc__
"""
S.islower() -> bool

Return True if all cased characters in S are lowercase and there is
at least one cased character in S, False otherwise.
"""
return False

def isnumeric(self): # real signature unknown; restored from __doc__
"""
S.isnumeric() -> bool

Return True if there are only numeric characters in S,
False otherwise.
"""
return False

def isprintable(self): # real signature unknown; restored from __doc__
"""
S.isprintable() -> bool

Return True if all characters in S are considered
printable in repr() or S is empty, False otherwise.
"""
return False

def isspace(self): # real signature unknown; restored from __doc__
"""
S.isspace() -> bool

Return True if all characters in S are whitespace
and there is at least one character in S, False otherwise.
"""
return False

def istitle(self): # real signature unknown; restored from __doc__
"""
S.istitle() -> bool

Return True if S is a titlecased string and there is at least one
character in S, i.e. upper- and titlecase characters may only
follow uncased characters and lowercase characters only cased ones.
Return False otherwise.
"""
return False

def isupper(self): # real signature unknown; restored from __doc__
"""
S.isupper() -> bool

Return True if all cased characters in S are uppercase and there is
at least one cased character in S, False otherwise.
"""
return False

def join(self, iterable): # real signature unknown; restored from __doc__
"""
S.join(iterable) -> str

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.
"""
return ""

def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.ljust(width[, fillchar]) -> str

Return S left-justified in a Unicode string of length width. Padding is
done using the specified fill character (default is a space).
"""
return ""

def lower(self): # real signature unknown; restored from __doc__
"""
S.lower() -> str

Return a copy of the string S converted to lowercase.
"""
return ""

def lstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.lstrip([chars]) -> str

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""

def maketrans(self, *args, **kwargs): # real signature unknown
"""
Return a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result.
"""
pass

def partition(self, sep): # real signature unknown; restored from __doc__
"""

Search for the separator sep in S, and return the part before it,
the separator itself, and the part after it.  If the separator is not
found, return S and two empty strings.
"""
pass

def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
"""
S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.
"""
return ""

def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rfind(sub[, start[, end]]) -> int

Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.
"""
return 0

def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rindex(sub[, start[, end]]) -> int

Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.

"""
return 0

def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""
S.rjust(width[, fillchar]) -> str

Return S right-justified in a string of length width. Padding is
done using the specified fill character (default is a space).
"""
return ""

def rpartition(self, sep): # real signature unknown; restored from __doc__
"""

Search for the separator sep in S, starting at the end of S, and return
the part before it, the separator itself, and the part after it.  If the
"""
pass

def rsplit(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.rsplit(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string, starting at the end of the string and
working to the front.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified, any whitespace string
is a separator.
"""
return []

def rstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.rstrip([chars]) -> str

Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""

def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
"""
return []

def splitlines(self, keepends=None): # real signature unknown; restored from __doc__
"""
S.splitlines([keepends]) -> list of strings

Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends
is given and true.
"""
return []

def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.startswith(prefix[, start[, end]]) -> bool

Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
"""
return False

def strip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""

def swapcase(self): # real signature unknown; restored from __doc__
"""
S.swapcase() -> str

Return a copy of S with uppercase characters converted to lowercase
and vice versa.
"""
return ""

def title(self): # real signature unknown; restored from __doc__
"""
S.title() -> str

Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case.
"""
return ""

def translate(self, table): # real signature unknown; restored from __doc__
"""
S.translate(table) -> str

Return a copy of the string S in which each character has been mapped
through the given translation table. The table must implement
lookup/indexing via __getitem__, for instance a dictionary or list,
mapping Unicode ordinals to Unicode ordinals, strings, or None. If
this operation raises LookupError, the character is left untouched.
Characters mapped to None are deleted.
"""
return ""

def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str

Return a copy of S converted to uppercase.
"""
return ""

def zfill(self, width): # real signature unknown; restored from __doc__
"""
S.zfill(width) -> str

Pad a numeric string S with zeros on the left, to fill a field
of the specified width. The string S is never truncated.
"""
return ""```

Common operation

Is the string immutable?

```>>> a = 'xiaoyafei'     #Replication of a variable, that is, pointing the variable to an address in memory space
>>> id(a)               #Look at the spatial address of variable a at this point
139742341438256
>>> a = 'xiaobaba'         #Modify a
>>> id(a)                   #Look again at the spatial address of variable a
139742341436848```

Why is a string immutable? We can clearly see that the spatial address of variable a is different twice, because when we modify it, we point a to another memory space address, and the value of xiaoyafei will be garbage collected.

swapcase() lowercase to uppercase, uppercase to lowercase

```>>> str = 'Hello World'
>>> str.swapcase()
'hELLO wORLD'```

capitalize() is capitalized and the rest is lowercase

```>>> str = 'helLo WORLD'
>>> str.capitalize()
'Hello world'```

casefold() Change all capitals to lowercase

```>>> str = 'HELLO WORLD!'
>>> str.casefold()
'hello world!'```

center() returns a string in the middle, filled with characters on both sides

```>>> str = 'hello world'
>>> str.center(50,'*')
'*******************hello world********************'```

count() counts the number of occurrences of characters

```>>> str = 'abcdfdawadsqasacsasasaa'
>>> str.count('a')
9
#You can also specify where to start and where to end
>>> str.count('a',0,10)
3```

endswith() determines whether to end with a string and return True or False

```>>> str = 'hello world!python'
>>> str.endswith('python')
True
>>> str.endswith('hello')
False```

expandtabs() expandtab key

```>>> a = 'a\tb'
>>> a
'a\tb'
>>> print(a)
a       b
>>> a.expandtabs(20)
'a                   b'```

find() lookup, return index if found, return - 1 if not found

```>>> name = 'xiaoyafei'
>>> name.find('o')
3
>>> name.find('g')
-1
#You can also specify the search scope.
>>> name.find('a',4,9)
5```

format() formatted string

```>>> info = 'my name is {0},i am {1} years old.'
>>> info.format('xiaoyafei',22)
'my name is xiaoyafei,i am 22 years old.'
#Variables can also be used
>>> info = 'my name is {name},my age is {age}.'
>>> info.format(name = 'xiaoyafei',age=22)
'my name is xiaoyafei,my age is 22.'```

index() returns the index value and fails to find it

```>>> s = 'hello tairan and welcome!'
>>> s.index('t')
6
>>> s.index('g')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
#It can also be found based on the start and end locations.
>>> s
'hello tairan and welcome!'
>>> s.index(
... 'a')
7```

Is isalnum() a judgment of Arabic characters (numbers and letters)?

```>>> a = '123abc'
>>> a.isalnum()
True
>>> b = '123!abc'
>>> b.isalnum()
False```

isalpha() judges whether it contains only words, not numbers

```>>> a = 'abc123'
>>> a.isal
a.isalnum(  a.isalpha(
>>> a.isalpha()
False
>>> b = 'abcdefg'
>>> b.isalpha()
True```

Is decimal() an integer?

```>>> a = '111'
>>> a.isdecimal()
True
#The second method
>>> a.isdigit()
True```

Is isidentifier() a variable name that can be used to determine the value of a string?

```>>> a = '111'
>>> a.isidentifier()
False           #Numbers cannot be used as variable names
>>> b.isidentifier()
True```

islower() determines whether the strings are all lowercase?

```>>> str = 'HelloWorld'
>>> str.islower()
False
>>> str2 = 'helloworld'
>>> str2.islower()
True```

isnumeric() judges whether only numbers are in it?

```>>> a = '1233'
>>> a.isnumeric()
True
>>> b = 'abc123'
>>> b.isnumeric()
False```

Is isspace() a blank?

```>>> a = ' '
>>> a.isspace()
True```

title() becomes title, that is, the initial letter of each word is capitalized.

```>>> str = 'hello world python tairan '
>>> str.title()
'Hello World Python Tairan '```

upper() converts all strings to uppercase
python >> namew = 'xiaoyafei' >> namew.upper() 'XIAOYAFEI'
join() converts the list into a string and distinguishes the elements in the list by the specified string

```>>> names = ['lipeng','likun','lipeng']
>>> str = '---'
>>> str.join(names)
'lipeng---likun---lipeng'```

ljust() starts on the left and replaces it with a specified character if the total length is insufficient

```>>> name = 'xiaoyafei'
>>> name.ljust(50,'_')
'xiaoyafei_________________________________________'```

lower() turns strings into lowercase

```>>> name = 'XIAOYAFEI'
>>> name.lower()
'xiaoyafei'```

strip() removes spaces and newlines on both sides

```>>> str = '\n hello world              '
>>> str.strip()
'hello world'
# lstrp goes to the left instead of the right
# rstrip goes only to the right, not to the left```

```>>> str_in = '1234567890'       #This is original.
>>> str_out = '!@#\$%^&*()'        #This is the output.
>>> table = str.maketrans(str_in,str_out)       #Corresponding the two tables
>>> s = '572428582'         #Re-input
'%&@\$@*%*@'```

partition() cuts the entire string as you want

```>>> str
'hello world'
>>> str.partition('o')
('hell', 'o', ' world')```

replace() by default

```>>> s = 'hello world,hello tairan'
>>> s.replace('llo','LLO')
'heLLO world,heLLO tairan'
#Replacement only once
>>> s
'hello world,hello tairan'
>>> s.replace('llo','LLO',1)
'heLLO world,hello tairan'```

rsplit() Cuts the string from the right to the specified character

```> s
'hello world,hello tairan'
>>> s.rsplit('o')
['hell', ' w', 'rld,hell', ' tairan']```

splitlines() are divided into rows in a list format

```>>> str = 'a\nb\nc\nd\n'
>>> str.splitlines()
['a', 'b', 'c', 'd']```

startswith() judgement starts with the specified character

```>>> str
'hello,xiaoyafei'
>>> str.startswith('hel')
True```

zfill() starts on the left and replaces it with the specified character (the original string on the right).

```>>> str = 'hello tairan'
>>> str.zfill(50)
'00000000000000000000000000000000000000hello tairan'```

All string operations are explained!

To be continued

Keywords: Linux encoding ascii Python REST

Added by Cornelia on Mon, 20 May 2019 04:30:59 +0300