Python basic syntax-(quotation marks, strings)

Source: Internet
Author: User

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

    1. 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)

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.