Python basic learning notes (2)

Source: Internet
Author: User
Tags escape quotes

Continue with the content in article 1,Python.

 

Note

 

To make it easier for others to understandProgram, Using annotations is very effective, even if you look back at the oldCodeThe same is true.

 
>>>#Get User Name:>>> User_name = raw_input ("What is your name?")

InPythonChina site (#. Well no (#) The memory on the right will not be executed by the program. The Code itself should be easy to understand even if there are no annotations. Fortunately!PythonIs an excellent language that helps programmers write easy-to-understand programs.Pai_^

 

 

String

 

 

Single quotes string and escape quotes

>>>"Hello, world"'Hello, world'>>>'"Hello, world" she said''"Hello, world" she said'>>>"'Hello, world' she said""'Hello, world' she said"

 

The first output is obviously a double quotation mark (""). How does one input a single quotation mark (''). What is the difference? In fact, there is no difference.

Let's look at the second one. The single Guide ('') contains a pair of double quotation marks (" "). This time the complete output is made. Is the output of double quotation marks a single quotation mark?

Look at the third one. Double quotation marks ("") contain a pair of single quotation marks (''). Why didn't the outermost double quotation marks be converted into single quotation marks for output this time? This compiler is emotional!

 

 
>>>"Let's go""Let's go">>>'Let'S go'Syntaxerror: Invalid Syntax>>>'Let \'s go'"Let's go"

The first output contains a single quotation mark (in fact, it should be called an apostrophes ).(')Because it does not appear in pairs .) Can be output normally.

The second output contains a single pair of single quotes (this should be the intent of the input ). The compiler does not know how to recognize it.

The third output, in order to achieve this goal, we need to use the single quotation mark in the middle with a slash (\. This time it is different. The single quotation mark output is changed to double quotation marks. Compiler, you are really naughty.

 

Concatenated string

Next try the plus sign (+) To splice a character

>>>"Helle,"+"World!"'Helle, world!'

>>> X ="Hello,">>> Y ="World!">>> X +Y'Hello, world!'

 

 

String representation,StrAndRepr

In the previous example, the reader may notice that allPythonThe printed string is enclosed in quotation marks. This is becausePythonThis value is maintained when the value is printed.PythonThe status in the Code, rather than the status you want the user to see. If you usePrintStatement, the results are different:

>>>"Hello, world!"'Hello, world!'>>> 10000lstml >>>Print "Hello, world!"Hello, world!>>>Print10000l10000

We can see that the number of long integersElse lConverted to numbers10000And also when it is displayed to the user.

Here we will discuss two mechanisms for converting values into characters. You can use the following two functions:

>>>PrintSTR ("Hello, world!") Hello, world!>>>PrintSTR (distinct L)10000 >>>PrintRepr ("Hello, world!")'Hello, world!'>>>PrintRepr (distinct L)Else l

STR ()Function, which converts the value to a string in a reasonable form, which can be understood by the user;

Repr ()Function, it creates a string, which is validPythonExpression to represent the value.

 

 

InputAndRaw_inputComparison

The last example in the previous chapter usesRaw_inputFunction.InputWhat is unnecessary? Next we useInputFunction. Try the example again.

>>> Name = input ("  What is your name?  "  ) What  Is  Your name? Huhutraceback (most recent call last): File  "  <Pyshell #0>  " , Line 1, In <Module> Name = Input ( "  What is your name?  "  ) File "  <String>  " , Line 1, In <Module> Nameerror: Name  '  Huhu  '   Is   Not  Defined >>> Name = input ( "  What is name?  "  ) What  Is Name? "  Huhu  " >>> Print   "  Hello,  " + Name + "  !  "  Hello, huhu! 

Input ()The function assumes that the input is valid.PythonExpression. Therefore, enterHuhuThe system will prompt an error, but if the quotation mark ("Huhu") It will be a valid character and there is no problem with running the program.

However, it is too much to require users to enter their names with quotation marks.Raw_inputFunction.

>>> Input ("Enter a namber:") Enter a namber:33 >>> raw_input ("Enter a namber:") Enter a namber:3'3'

Of courseInputThere are special needs, such as when a user is required to enter a number.

 

 

Long String 

If you want to write a very long string that must span multiple rows, you can use three quotation marks to enclose the regular quotation marks.

>>>Print '''This is a very long string. It continues here. And it's not over yet.'''ThisIsA very long string. It continues here.AndIt'S not over yet.

Common strings can also be cross-row. If the last character in a line is a backslash, The linefeed itself is "escaped", that is, ignored.

 
>>>Print "Hello. \ world!"Hello. World!>>> 1 + 2 +\4 + 512

 

 

 

Original string

 
>>> Path ='C: \ ABC'>>>PrintPathc: BC

How can this happen? I want to input a path, but it is wrapped in a line break.

>>>Print 'C: \ ABC'C: \ ABC>>>Print 'C: \ Program Files \ fnord \ Foo \ bar \ Baz \ frozz'C: \ Program Files \ fnord \ Foo \ bar \ Baz \ frozz

Through the above double slash (\\) Solved the path problem, but would it be too troublesome if the path is too long.

>>>PrintR'C: \ Program Files \ fnord \ Foo \ bar \ Baz \ frozz'C: \ Program Files \ fnord \ Foo \ bar \ Baz \ frozz>>>PrintR'Let \'s go!'Let \'S go!

The original string does not treat the backslash as a special character. As you can see, the original string usesR.

 

 

UnicodeString

The last type of A String constant isUnicode String (or Unicode Object --- Is not of the same type as the string ). Python In 8 Bit ASCII Code to form storage, and Unicode Strings are stored 16 Bit Unicode Character, which can represent more character sets, including special characters in most languages in the world.

If you are not short, what isUnicode, AccessibleUnicodeWebsite:Http://www.unicode.org

 
>>> U'Hello, world!'U'Hello, world!' 

As you can see,UnicodeString usageUPrefix, just as the original string usesRSame.

Note: InPython 1, 3.0All strings areUnicodeString.

 

 

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.