Python Basics (ii) data types and Operations (2)--string

Source: Internet
Author: User
Tags escape quotes

String Basics

Python also provides strings that can be represented in several different ways. They can be identified by single quotation marks ( ‘...‘ ) or double quotation marks ( "..." ). \can be used to escape quotes:

>>>'Spam eggs'  #Single quotes'Spam eggs'>>>'doesn\ ' t'  #Use "to escape " the single quote ..."doesn ' t">>>"doesn ' t"  #... or use double quotes instead"doesn ' t">>>'"Yes," he said.''"Yes," he said.'>>>"\ "Yes,\" he said."'"Yes," he said.'>>>'"Isn\ ' t," she said.''"Isn\ ' t," she said.'

In the interactive interpreter, the output string is enclosed in quotation marks, and special characters are escaped with backslashes. Although it may not look the same as the input, two strings are equal. If there are only single quotes in the string and no double quotes, use double quotation marks, otherwise quoted in single quotation marks. The print () function produces a more readable output that omits quotes and prints out special characters that are escaped:

>>>'"Isn\ ' t," she said.''"Isn\ ' t," she said.'>>>Print('"Isn\ ' t," she said.')"Isn ' t,"she said.>>> s ='First Line.\nsecond line.'  #\ means newline>>> s#without print (), \ n is included in the output'First Line.\nsecond line.'>>>Print(s)#with print (), \ n produces a new lineFirst line . Second line.

If \ the character you have preceded is treated as a special character, you can use the original string by adding one before the first quotation mark r :

Print ('C:\some\name')  # Here \ means newline! C:\someame Print (R'C:\some\name')  # Note the R before the quote C:\some\name

string multi-line output

String literals can be divided into multiple lines. One way is to use three quotes: """...""" or ‘‘‘...‘‘‘ . Line end newline characters are automatically included in the string, but can be added at the end of \ a row to avoid this behavior. The following example: You can use a backslash to end a row with a continuous string, which indicates that the next line is logically the following for us:

1 Print ("" "2usage:thingy [OPTIONS]3-     H                        Display this Usage Message4-     h hostname               hostname to connectto5"" ")

The following output will be generated (note that there is no beginning of the first line):

usage:thingy [OPTIONS]      -H                        Display This usage message     -h hostname               hostname to connect to

String Formatted output

string concatenation of all evils :    the string in Python is represented in the C language as a character array, and each time a string is created, it needs to open a contiguous space in memory, and once you need to modify the string, you need to make room again, and the Evil + sign will re-open up a space within each occurrence.

Strings can be + concatenated (glued together) by operators, and can be * repeated by means of:

# 3 times ' un ', followed by ' Ium ' ' un ' ' ium ' ' Unununium '

Adjacent two strings of text are automatically concatenated together. :

' Py ' ' Thon ' ' Python '

It is used only for two string literals and cannot be used for string expressions:

' Py ' ' Thon '  # can ' t concatenate a variable and a string literal   ... Syntaxerror:invalid Syntax>>> ('un'ium '   ... Syntaxerror:invalid Syntax

If you want to concatenate multiple variables or concatenate a variable and a string literal, use + :

' Thon ' ' Python '

This feature is especially useful when you want to slice a long string:

>>> Text = ("to" has            them joined together. ' )>>> text'Put several strings within parentheses to has them joined Together. '

string concatenation formatted output

Name = input ("name:"= input ("age:"= input ( " Job: " Print("" "-------------info-----------Name:" "+name+ """ Age : "" "+age+" ""Job:"" "+job"

Input

name:brainage:job:it

Output

-------------info-----------name:brainage:job:it

Python Basics (ii) data types and Operations (2)--string

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.