0 Fundamentals python-7.2 String Constants

Source: Internet
Author: User

1. Single-and double-argument strings are the same

When your Python writes according to the example above, this time the single-and-double-argument string is the same. The two can be interchangeable.

We can even introduce each other directly in a single-pair

>>> ' abc ' d ', "ABC ' d" (' abc ' d ', "abc ' d")

In a casual expression, Python will voluntarily merge the concatenated strings, although they are not connected by +

>>> ' abc ' d ', "ABC ' d" (' abc ' d ', "abc ' d")

Also, just like the examples above, add commas to the middle of the string. The final form is a tuple, not a string.

2. Using escape characters to represent special characters

Look at the example above, with the escape character between the strings, assuming that we print directly with the variable, he will print all the characters, but suppose print is used. The escape character becomes a binary value from the original string, such as: \ n for tabs, and so on

Common escape characters:

Escape Significance
\ Continuous. Then the top line
\\ Back slash
\‘ Single cited
\" Double cited
\a Bell
\b Retrogression
\f Page change
\ n Line break
\ r Return
\ t Horizontal tab
\v Vertical tab
\n{id} Unicode database ID
\uhhhh Hexadecimal value of the UNICODE16 bit
\uhhhh Hexadecimal value of the UNICODE32 bit
\xhh Hexadecimal value
\ooo Eight-binary value
/ Null
\other Do not escape

3.raw string Suppression Escape

There are very many times when we need to open files and so on, then we need to enter the path. Especially the path of window. Most use backslashes, this time there will be a failure

Like what:

Handler=open (' C:\nb123.txt ', ' W ')

There's a problem at this point. Originally opened nb123 This text file, but because there is a backslash in front, in escaping it is a newline, causing ambiguity

So we need to change the path to look like this:

Handler=open (R ' C:\nb123.txt ', ' W ') handler=open (' C:\\nb123.txt ', ' W ')

This time is legal, use R to suppress escape. Or use a double-backslash

4. Use triple-double-quotes to enter multiple strings

>>> a= "" "Aaabbbccdcdef ' Ddfdfd" "" >>> A "Aaabbb\nccdcdef ' DDFDFD"

It is often used for document strings, or to stare at large sections of code

Here it is. Thank you


Click the jump 0 Basic python-folder

0 Fundamentals python-7.2 String Constants

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.