One, single quote string and escape quotation mark
When single quotation marks appear in a string, we can enclose the string in double quotation marks: "Let's go!"
When double quotation marks appear in a string, we can enclose the string in single quotation marks: ' "hello,world!" she said '
But what to do when there is a single quote ' and double quotation mark ' in the string: use a backslash (\) to escape the quotation marks in the string: ' let\ ' s go! '
Second, string
- Stitching strings
>>> "Let's Say" ' "hello,world!" "Let\ ' s say" hello,world! " ‘
>>>x= "Hello,"
>>>y= "world!"
>>>x y
Syntaxerror:invalid syntax
>>> "Hello," + "world!"
' hello,world! '
>>>x+y
' hello,world! '
The above is just one way to write two strings, Python will automatically stitch them, but if the assignment to the variable and then splicing in this way will be an error, because this is just a special way to write a string, not a common method of stitching strings, this mechanism is not used much. with "+" good can be the concatenation of strings;
2. String representation, str and repr
>>>print repr ("hello,world!") ' hello,world! ' >>>print repr (10000L) 10000l>>>print str ("hello,world!") Hello,world!>>>print Str (10000L) 10000
STR, like int and bool, is a type, and repr is just a function, repr (x) can also write an ' X ' implementation (note that ' is an anti-quote, not a single quote); however, the anti-quotes are no longer used in Python3.0. So, even in old code, you should stick with repr.
Comparison of 3.input and Raw_input
Input assumes that the user entered a valid Python expression, such as entering a value of 1, the program will not be considered as STR, but as an int type, enter x, the program will be as the user input is the variable x, if the input "x", the program will be a string of people;
The Raw_input function it treats all inputs as raw data and then puts them into a string.
>>> Name=input ("What's your Name?"); Print Namewhat is your name? Allentraceback (most recent): File "<pyshell#22>", line 1, in <module> name=input ("What's Your name? "); Print name File "<string>", line 1, in <module>nameerror:name ' Allen ' are not defined>>> NAME=INP UT ("What is your name?"); Print Namewhat is your name? " Allen "Allen>>>input (" Enter a number: ") Enter a Number:33>>>raw_input (" Enter a number: ") Enter a number : 3 ' 3 '
Python basic syntax-(quotation marks, strings)