Python 6 basic functions of numeric and Boolean values and strings

Source: Internet
Author: User

Data type: View variable data types type (variable) or print (type (variable))

integer int : A natural number without decimals, also called an integral type. In the 2. The x version is also divided into long integers and shaping. But in 3. Unity in the X version is called an integer or integral type.

Create: A = 123 or a = Int (123) The result is the same as the integer a = "123" is the string

Conversion: new_a = Int (a) conversion can only be converted. For example, if A is ABC, then this cannot be converted to int type.

boolean bool : used primarily for condition determination after the if elif while.

True: You can assign a value to a variable such as a = True

False: You can assign it to a variable such as a = False

conversion:BOOL (variable or number) is only 0 or empty, the result is false when converted to a bool value.

a few special conversions:

Name = ""

name1 = "Space"

A = bool ("space") converts a space directly to bool to get false.

A = bool (NAME1) Converts the variable to a space, and the bool will get true.

A = bool (name) variable the value of name does not exist, at which time the conversion result is false

string str :

Created: a = "123", a = ' 123 ', a = str (123) A = str (123)

Conversion: b = 123 A = str (b)

string concatenation: a = "Hello" b = "World" A + b = ' HelloWorld '

string formatting:%+[s.d , etc.]. % is called a placeholder:

①a = "This is%s year,%s month%s Day"% (2017,4,29) The final result is a = "This is 2017, April 29."

The ② percent is called a placeholder, the red% is the delimiter, and the subsequent 2017 4 29 will correspond to the placeholder in the replacement quotation mark if there is only one

The number of values to be mapped in there is no parentheses behind.

The s that follow the ③ percent sign are formatted as strings for the characters that are mapped to%, and d for formatting as numbers.

You can also write as follows:

A = "This is%s year,%s month%s Day"

New_a = A% (2017,4,29)

Result New_a = "This is 2017, April 29"

string manipulation:

Remove or replace left and right spaces and \ n newline characters:

For example: A = "ABC" a_new = A.strip (value x).

Srtip replace both spaces with the value x Lstrip replace the left space as the value x Rstrip replace the right space with the value x. The value can be empty.

Value: Can be filled with nothing this is tantamount to removing both sides of the space, if filled with that is to replace both sides of the space.

Example 1:a = "abc" a_new = A.strip () The result is a_new = "abc" Remove space

Example 2:a = "ABC" a_new = A.strip (@) result is a_new = "@[email protected]" replace both ends with a space of @

Example 3: A = "ABC" a_new = A.lstrip (@) result is a_new = "@ABC" replaces the left space is @

Example 4:a = "ABC" a_new = A.rstrip (@) result is a_new = "[email protected]" Replace right space @

Split: variable name. Split (' split boundary identifier ') split or rsplit from right to left.

Example: Name = "Admin*armin*guest"

Example 1 new_name = Name.split (' * ') where. is the dividing line, the result is new_name = [' admin ', ' Armin ', ' guest ']

Example 2 new_name = Name.split (' * ', 2) meaning left to right is divided into the 2nd * symbol. [' admin ', ' Armin ', ' guest ']

Numbers with negative values are the same as the numbers that are not written.

Example 3 new_name = Name.rsplit (' * ', 2) meaning right to left has been divided into the 2nd * symbol [' admin ', ' Armin ', ' guest ']

Length: The length in Python3 is calculated according to the number of characters. Python2 Chinese is calculated by 2 characters.

name = ' Armin ' V = len (name) print (V) can see a length of 5. Interactive mode len (variable name) to see the length.

Index: Used to remove a specific value

Name = "Armin" V = name[1] Print (v) is R. The code means that the value of position 1 is taken out of the Armin,

The position is counted from 01234, and 0 represents the first bit.

Loop out the values in turn:

'Armin'i = 0 whilei < 5: v =print(v) i + = 1       

Slices: As with the index function, it is to get out the value. The difference is that the slice takes a character, so only one character can be taken.

The rule of value: The ① packet before the packet is 0:4 actually only 1234 locations. [Start: End: Step x] Start with

The end must be on the left side of the end compared to the position.

② step span is X-1, that is, if you enter 2, then take the first one after the middle one and then take down one

Analogy. 0::2 means to take the value of step 2 from beginning to end.

Left to right position ordinal 0 1 2 3 4

String a r m i n

Right-to-left position sequence number-5-4-3-2-1

"Armin" name = Name[0:4] means to  take 1-3 bits of name and assign to nameprint'Armin'print (name[ 0:2]) Prints the 1-2 bits in name. 

Python 6 basic functions of numeric and Boolean values and strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.