Into the world of Python (v) data type 2. Strings (String)

Source: Internet
Author: User
Tags printable characters

Strings string

A set of quotation marks that can contain a collection of numbers, letters, symbols (non-special system symbols).

Such as:

Strval= "This is a test!" Strval1= ' This is a test! ' Strval2= "" "This is a test" ""
Sanchong quotation marks (docstring)

Python Sanchong quotation marks allow strings to span multiple lines, including literal line breaks, tabs, and other special characters.

The Sanchong quotation mark syntax consists of three consecutive single or double quotation marks.

#!/usr/bin/pythonpara_str = "" "This is a long string of made up ofseveral lines and non-printable characters such AsT AB (\ t) and they would show up the that's the displayed. Newlines within the string, whether explicitly given likethis within the brackets [\ n], or just a NEWLINE withinthe vari Able assignment'll also show up. "" " Print para_str;

When executing the above code, the following results are produced. Note how each special character is converted to the print form, one until the last line break between the string "up" closing the triple quotation mark. Also note that a newline occurs or that uses an explicit carriage return at the end of a line or its escape code (\ n):

This was a long string that was made up ofseveral lines and non-printable characters such astab () and they would show up That is when displayed. Newlines within the string, whether explicitly given likethis within the brackets [], or just a NEWLINE withinthe variabl E assignment'll also show up.



It is important to note that Python will consider the contents of the paired quotation marks to be the string content.

The following example will have a syntax error

Say= ' Let's Go ' File ' <stdin> ', line1 say= ' let ' s go ' ^ syntaxerror:invalid syntax


The right approach is

If you include only single quotes: use double quotation marks to include strings

If you enclose a single quotation mark, double quotation marks: use double quotation marks to enclose the string, and double quotes inside the string are escaped with the escape character \

Say= "Let ' s \" Go "


Escape character

The following table is a list of non-printable characters that are not escaped and can be represented by a backslash symbol.

The escape character is interpreted in a single-quote and double-quote string.

Reverse Slash

Symbol

Hexadecimal

Character

Describe
\a 0x07 Ringtones or alarms
\b 0x08 Backspace key
\cx
Control-x
\c-x
Control-x
\e 0x1b Escape
\f 0x0c Page change
\m-\c-x
Meta-control-x
\ n 0x0a New Line
\nnn
octal notation, where N has a range of 0.7
\ r 0x0d Enter
\s 0x20 Space
\ t 0x09 TAB key
\v 0x0b Vertical tabulation
\x
Character X
\xnn
hexadecimal notation, where the range of n is from 0.9,A.F, or AF


String Special Operators
operator Description Example
A= Hello B=python
+ Concatenation-increases the value on both sides of the operator

The value of a + B is Hellopython

* Duplicate-Creates a new string that links multiple copies of the same string The value of a*2 is-hellohello
[] Slice-Specifies from the given index character The value of a[1] is e
[:]

Range Slice-characters within a given range

can be given three values, starting index, end index, step value

End value before the index will be taken out

Omit starting index, starting from 0

Omit end index, to end.

If the index value is negative, the

Step value from the end indicates that there is no interval between the number of strings taken when the slice range is taken, and the default value is 1

If the step value is negative, you can also indicate from right to left

A[1:4] is  , ell

A[:4] has a value of Hell

a[1:] The value is Ello

a[-4:-1] has a value of Ello

a[::2] Hlo

Inch Member-Returns True if a character exists in the given string H in a result value is 1
Not in Member-Returns TRUE if the input character does not exist in the given string M Not in a result value is 1
R/r Raw string-disables the actual meaning of the escaped character. The syntax primitive string is exactly the same as the normal string, except for the original string operator, the letter "R", which precedes the quotation marks. In "R" can be lowercase (r) or capital letter (R), must be placed immediately before the first quotation mark. Print R ' \ n ' printing \ n and print R ' \ n ' printing \ n


String formatting operations

The coolest feature of Python is the string format operator%. This operation is the only string that complements the C-language printf () series function function. The following is a simple example:

#!/usr/bin/pythonprint "My name is%s and weight is%d kg!"% (' Zara ', 21)

When executing the above code, the following results are produced:

My name is Zara and weight are kg!

The following is the complete, it can be used with the% symbol list:

format Symbols Conversion
%c Character
%s formatted with the STR () string conversion
%i Signed decimal integer
%d Signed decimal integer
%u unsigned decimal integers
%o Eight-binary integers
%x hexadecimal integers (lowercase letters)
%x hexadecimal integers (uppercase letters)
%e Index symbol (lowercase ' e ')
%E Index symbol (uppercase "E")
%f Floating point Real numbers
%g Shorthand for%f and%e
%G Shorthand for%f and%e

Other supported symbols and functions are shown in the following table:

symbols function
* Parameters specify width and precision
- Align Left
+ Display symbols
<sp> Leave a blank before a positive number
# Add a leading 0 (' 0 ') or hexadecimal leading 0x "or" 0X ", depending on whether the" X "or" X "uses octal.
0 Add 0 (not spaces) from the left side of the keyboard
% ' percent ' to leave a word '% '
(VAR) mapping variables (dictionary parameters)
M.N. M is the smallest total width and n is the number that shows the number of digits after the decimal point (if appl)
Unicode string

In Python, ordinary strings are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This makes a group of characters more diverse, including the special characters of most languages in the world. Restricting processing of Unicode strings

#!/usr/bin/pythonprint u ' Hello, world! '

When the above code is executed, the following results are produced:

Hello, world!.

As you can see, the Unicode string uses the prefix u, just like the original string using the prefix R


Into the world of Python (v) data type 2. Strings (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.