Python-06:python Syntax Basics-Number and string

Source: Internet
Author: User

1, Number of types

The types of Python are mainly 5, there are integers (int), long Integer (long), floating-point (float), Boolean (bool), complex (complex)

For example: 0-1 1-999 999 These are all integer types.

87959L-87136L etc. these are long integers (followed by a l[note: lowercase l])

3.14159 6.6668 floating-point with decimal point

BOOL type has only two values, not true false

The plural type is the general XXX+XXXJ of mathematics such as 3+5j etc.

2. What is a string

The character set in Python, enclosed in quotation marks (single quotes, double quotes, and three quotation marks), is called a string, for example: "Xiaoxin", "123", "3+59", "Jiliguala" and so on.

Single quotes: double quotes can be used in single quotes and can be output
 

# encoding:utf-8s1='123'print  s1  #123s2= '  'print  s2  #

Double quotes: You can use single quotes and output them in double quotes. It means double quotes and single quotes can be used to cross

# encoding:utf-8s1="123"print  s1  #123s2= " it ' s a dog " Print  S2  #it ' s a dog

Three quotation marks: the string enclosed in three quotation marks can be wrapped, and the single and double quotation marks cannot be (three quotes can be three double quotes and three single quotes)

# encoding:utf-8s1='goodgoodstudy,dayday up! " " Print  S1

The output result is

3. Escape character

Put a single quote in the single quotation mark will be an error, because three single quotes, the program does not know who matches who

To solve this problem, except that the outer quotation marks are changed to double-quoted and the escape character

, the escape character \ means that a symbol after the \ symbol represents its original meaning.

  

\ n is the meaning of line break and the application of escape character

4. Natural string

In Python, if our string has an escape character to keep it as is, without any processing, we can use a natural string, which is preceded by a string of R

  

5. Repetition of strings

If we are going to duplicate the output of a string, you can use the repeating operator of the string in addition to the manual input.

where \ t represents a tab distance and is also an application of the escape character

6. Substring

If we want to intercept a substring in a string, we need to do a substring operation.

There are two main methods of operation, one is index cloud algorithm [], one is slicing cloud algorithm [:]

  

#Encoding:utf-8" "The sub-string index operator starts at 0 index the slice operator [A:B] refers to starting from the subscript a subscript to the b-1 subscript, the same as the first bit subscript is 0" "C1='Jiyongxinnihao'PrintC1C2=C1[0]Printc2c3=c1[5]PrintC3C4=c1[:2]Printc4c5=c1[2:]PrintC5c6=c1[4:7]PrintC6

The output is:

  

Python-06:python Syntax Basics-Number and string

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.