Python Tutorial: Introduction to Python

Source: Internet
Author: User

Python Tutorial: Introduction to Python


1. The Python command line interpretation prompt

Enter control + P command prompt to look up

Enter CONTROL + N command prompt to look down


2, in interactive mode, the value of the last printed expression is given to the variable _


3. Add r characters before the first quotation mark of the string to avoid passing \ Escape characters

Print R ' C:\some\name '


4, use three quotation marks contain the string can span more than one line

“””...””"

‘’’...’‘‘


Note: The first row of the string automatically contains the end of line newline character, which can be avoided by adding \ At the beginning of the

print "" \

usage:thingy [OPTIONS]

-H Display This usage message

-h hostname hostname to connect to

"""


5. String connection (+, *)

' abc ' + ' Def '# string connection, can connect variable

' ABC ' * 3 # string Repetition

' Py ' ' thon '# Two string literals automatically connected, not including variables or expressions


# string Connection

>>> Text = (' Put several strings within parentheses '

' To has them joined together. ')

>>> text

' Put several strings within parentheses to has them joined together. '


6. String index

The index of the string is indexed from 0, the string is a type without a split character, and a character is a simple length of 1 strings

>>> word = ' Python '

>>> Word[0] # character in position 0

P


7. Negative numbers start counting from the right side of the string

>>> Word[-1] # last character

N

Note: 0 is equivalent to 0, negative number starts from-1


8, string support slice, index get single character, slice get substring

>>> Word[0:2] # characters from position 0 (included) to 2 (excluded)

' Py '

>>> Word[2:5] # characters from position 2 (included) to 5 (excluded)

' Tho '

Note: The start parameter of the slice is always included, and the end is always excluded.


9, the string slice default value, the first index omitted the default is 0, the second index omitted the default is the length of the slice;

>>> Word[:2] # character from the beginning to position 2 (excluded)

' Py '

>>> Word[4:] # characters from position 4 (included) to the end

' On '

>>> Word[-2:] # characters from the Second-last (included) to the end

' On '


10, the simplest understanding of the string slicing principle is to remember the position between the characters, the left side of the character is 0, the right side of the character is n is index N:

+---+---+---+---+---+---+

| P | y | T | H | o | n |

+---+---+---+---+---+---+

0 1 2 3 4 5 6

-6-5-4-3-2-1


11. Using a larger index will cause the following error

>>> word[42] # The word only has 7 characters

Traceback (most recent):

File "<stdin>", line 1, in <module>

Indexerror:string index out of range


12, Python string can not be modified, to the string index position assignment will appear the following error!

>>> word[0] = ' J '

...

TypeError: ' str ' object does not support item assignment

>>> word[2:] = ' py '

...

TypeError: ' str ' object does not support item assignment


# If necessary, you can create a new string.


13. Python 2.0 introduces new data types for storing text, Unicode objects. He can store, maintain, and provide automatic conversion of Unicode data.

Unicode is often used to solve internationalization.


14. Creation of Unicode strings

>>> u ' Hello world! '

U ' Hello world! '


# The lowercase u in front of the string is supported for creating Unicode characters, if you want to use special characters, refer to Unicode-escape. For example:

>>> u ' Hello\u0020world! '

U ' Hello world! '


Note: \u0020 represents Unicode character 0x0020 (space)


15.










This article is from the "Maple Snow" blog, please be sure to keep this source http://cnhttpd.blog.51cto.com/155973/1660235

Python Tutorial: Introduction to Python

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.