Chapter. 2.2. Python built-in data structure: strings, bytes, and byte arrays

Source: Internet
Author: User
Tags iterable throw exception

String

An ordered sequence, a collection of characters

Sequence of characters using single quotation marks, double quotation marks, and triple quotation marks

The string is an immutable object, and the merged string we see actually returns a value that is returned after the original two value is copied, which takes up new space.

From Python3, the string is a Unicode type

S1 = ' String '

S2 = ' This ' s a "string"

S3 = r "Hello \ n aasdasd": R and R indicate ignore escape

S4 = R ' Windows \net '

sql = "" "Select*from User where name= ' Tom '" "": Wrap in three quotes

string element supports subscript access

A space in a string is also a string

Orderly, can iterate

L1=list (SQL), putting element iterations into the list

L2=[sql], put the whole into the list

Join

Joins can iterate over objects, use the given string concatenation, return a string, iterate over the object itself, and the elements are strings.

"Sting". Join (iterable)-->str return string

', '. Join (Map (Str,range (10))), converting 1-9 to a string, separated by commas.

The map function, map (function,iterable,...), maps the specified sequence according to the provided function, processes each element in an iterative object, and returns a new list.

Print ("\" ". Join (LST)) #分隔符是双引号

Print ("\ n". Join (LST))

' \ n '. Join (' 1 ', ' 2 ', ' 3 ') is divided by \ n and can be printed using

String

+--> str: CONCATENATE two strings to return a new string .

String segmentation

Spilt Department Return to list

Split (Sep=none,maxsplit=-1),

Sep=none is cut by default with a space string character, with multiple spaces in one handle, and \t\n\r as whitespace characters.

The maxsplit represents the maximum number of knives, from left to right, and the default-1 means how many cuts can be cut.

Divides the string into characters by delimiter, returns the list, and returns the entire string in the list if there are no cut points

Note the escape character. Split with \ n to divide the content into line breaks.

Join and split are the opposite pair of actions

' \ n '. Join ("@". Join ([' 1 ', ' 2 ', ' 3 ']). Split (' @ ') +[' 4 ']): Connect ' 1 ' 2 ' 3 ' to ' @ ', then ' @ ', and ' 4 ' to return the new list to \ n Split.

Rsplit: Inverted, split from right to left,

Splitlines (keepends=false): to \ n Cut, no non-cut, keepends open to keep line breaks, line breaks have \r,\n,\r\n, etc.

Partition Departmentreturns a tuple

: Divides a string into two paragraphs, returns 2 segments, and a delimiter tuple.

Can be used to split the phone number, file directory.

An empty string will be an error as a delimiter, you must specify a separator

Rpartition: Inverted, if there is no cutting character, cut into the entire string and two empty strings

Partition and split are appropriate for small-scale use, and once the data size increases, space usage increases, and each cut returns a new list.

String case
Upper: All Caps

Lower: All lowercase

Use when judging, available when handling user input

Swapcase: Interactive case

String typesetting

Title ()--> str: Title Word capitalization, all capital letters

Capitalize ()-->STR: First word capitalization

Center (Width[,fillchar])--and Str: centered, with Fillchar padding, width

Zfill (width) print width, right with 0 padding

Ljust (WIDHT)--> Align Left

Rjust (width)--> Align Right

All of the above in the command interface, in the display to the customer, there must be interactive interface, or on the Web page.

String modification

Replace (Old,new[,count])-->STR

The strings in the replacement string are new substrings, the new string is returned, count represents the number of replacements, and all is replaced without specifying.

If the new string character contains more characters, in the string detection, it is a backward, not backward, such as replacing AA in AAA, from left to right to the left of the AA, the third A does not recalculate the front of the detected.

Strip ([chars])--> str

Removes all characters from the specified character set from both ends, if chars is not specified, takes out white space characters at both ends, and if the specified string is a space, only the space is removed, not all white space characters

Once you find an element that is not in the character set, stop.

Lstrip: From the left side start removal

Rstrip: Remove from right end

Used to remove extra characters from the data and return a new string.

Empty string is empty, white space is a blank string

String Lookup

Find (Sub,[,start[,end]) can specify interval, search from left to right for specified substring, find return positive index, otherwise return-1

RFind (Sub,[,start[,end]]) finds a substring sub from right to left, returns a positive index, or returns 1

The index is returned when the first one is found, and if not, it traverses

Index (Sub,[,start[,end]])-->int: Can specify the interval, within the interval from left to right to find the string, find the return index, otherwise throw an exception valueerror

Rindex (Sub,[,start[,end])-->int: Can specify the interval, in the interval from right to left to find the string, find the return index, or throw an exception valueerror

Both find and index are time-complex O (n), with increasing data size and reduced efficiency

Len (String): Returns the length of the string, the number of characters

String judgments

Endwith (Suffix[,start[,end])--> bool: Determines whether the string is the end of the suffix at the specified interval, and returns the Boolean value

Startwith (Prefix[,start[,end])--> bool: Determines whether a string is prefix at a specified interval, returns a Boolean value

Find (' www ', 0, 3): Often this way of judging the beginning, does not traverse

Can be used to determine the file type, whether to end with. Txt.jpg.pdf, left closed right open interval

Isalnum ()--bool whether alphanumeric composition

Isalpha () Whether it's a letter or not.

Isdecimal () contains only decimal digits

IsDigit () whether all digital 0~9

Isidentifier () Whether the letter underlines the beginning, the others are letters, numbers, underscores, judging identifiers

Islower

Isupper

Isspace () contains only white space characters

String formatting

Join stitching uses separators and requires stitching objects to iterate

+ Require conversion of non-string format to string format

Printf-style formatting the printf function from the C language

Placeholder is expressed in%

%03d means print three positions, not enough front 0, D for number format, s for string format

%3.2f 3 Indicates the width, the number of digits is greater than the width of the display, the number of digits can not be changed. 2 indicates precision, two digits after the decimal point, rounding

% #x% #X 16, x for lowercase, x for uppercase

'%-05d '% 10 occupies 5 width, right 0, if there is no decimal point, fill the space, no minus sign, on the left complement 0

Can look up the manual, the keyword str method

The Format function formats the string syntax

' {}{xxx} '. Format (*args,*kwargs)-->str

The args positional parameter, which is a tuple

Kwargs is a keyword argument, a dictionary

Curly brace placeholder, {n} positional parameter represents the value of positional parameter index n

{XXX} indicates that the search name is consistent in the keyword argument, {{}} indicates curly braces

Position parameters

' {}:{} '. Format (' 192.168.1.100 ', 8888) corresponds by location

' {1}{0} '. Format (' A ', ' B ') output as ' BA '

Access element ' {0[0]}. {0[1]} '. Format ((' magedu ', ' com ') output is magedu.com

Accessing Object Properties

From collections Import Namedtuple
Point=namedtuple (' point ', ' X y ')
P=point (4,5)
' {{{0.x},{0.y}}} '. Format (P)

Output {4,5}

Snap To

' {0}*{1}={2:<02} '. Format (3,2,2*3) print 3*2=6, output after left-shift 0 output as ' 3*2=60 '

Change 0 to # can also ' {0}*{1}={2:#<2} '. Format (3,2,2*3), Output ' 3*2=6# '

In-process

"Int{0:d};hex{0:x};oct{0: #o};bin{0: #b}". Format (42) output as ' int42;hex2a;oct0o52;bin0b101010 '

octets=[192,168,0,1]

' {: 02x}{:02x}{:02x}{:02x} '. Format (*octets)

Output as ' c0a80001 '

Floating point number

Print (' {: g} '. Format (3**0.5)) precision G and can also be f-Output 1.73205

Print (' {: 3.3%} '. Format (1/3)) width is three, three bits after the decimal point output 33.333%

Width cannot change the size of a number

Reversed (), the list does not change from the list, and the. Reverse () List changes

byte and byte arrays

Bytes: Byte sequence, non-volatile

ByteArray: byte array, variable

Strings and Bytes: strings are ordered sequences of characters that can be understood using encodings

Bytes is a non-variable sequence of bytes

ByteArray is a variable sequence of byte composition

Encoding and decoding

The string returns the byte sequence bytes according to the different character set encoding encode:

A.encode (encoding= ' utf-8 ', errors= ' strict ')-->bytes

BYTE sequence decoding decode return string according to different character sets

Bytes.decode (encoding= ' utf-8 ', errors= ' strict ')-->str

Bytearray.decode (encoding= ' utf-8 ', error= ' strict ')-->str

ASCII: U.S. standard code for Information interchange

A single-byte encoding system based on Latin alphabet

A byte 8 bits, a total of 256 variations, from the 0-255,ASCII Code of 0-127 is commonly used

Hexadecimal hex Indicates yes, 30 is the number 0,40 is the last digit of a, 60 is the previous one

Bytes Definition

Bytes ()

bytes (int) Specifies the byte of the bytes, which is filled by 0

Bytes (iterable_of_ints)-->bytes[0,255] int iterator object, over bounds error bytes must is in range (0, 256) left closed right open

Bytes (string,encodeing[,errors])-->bytes equivalent to String.encode () eg:bytes (' A ', encoding= ' utf-8 '), output is B ' a '

Bytes (bytes_or_buffer)->immutable copy of bytes_or_buffer copies a new immutable bytes object from a sequence of bytes or buffer

Using the B prefix definition

Allow only basic ASCII to use character form B ' Ac99 '

Use 16 binary to represent B ' \x41\x61 '

Bytes operation is similar to STR

The operation byte must be plus B.

Replace,find,

Bytes.fromhex (String): string must be a hexadecimal number of two characters, whitespace ignored

' abc '. encode (). Hex () returns a string of 16 binary representations,

Index b ' abcdef ' [2] returns the corresponding number of bytes, int type

ByteArray

ByteArray () Empty ByteArray

ByteArray (int) Specifies the byte of the bytes, which is filled by 0

ByteArray (iterable_of_ints)-->bytes[0,255] int iterator object, over bounds error bytes must is in range (0, 256) left closed right open

ByteArray (String,encoding[,errors]) returns a Mutable object

Note that the B prefix defines the bytes type

ByteArray operation

Replace,find

Bytearray.fromhex (String): string must be a hexadecimal number of two characters, whitespace ignored

ByteArray (' abc '. encode ()). Hex returns a string of 16 binary representations

Index ByteArray (b ' abcdef ') [2] returns the corresponding number of bytes, int type

Append tail Append

Insert (Index,int) inserts an element at the specified index position

Extend (iterable_of_ints) appends an iterative set of integers to the current ByteArray

Pop (Index=-1) popup element, default trailer

Remove (value) finds the first value removed, no throw exception found

Clear () Clear

Reverse () Flip, in-place modification

Int.from_bytes (Bytes,byteorder)

To represent a byte array as an integer

Int.to_bytes (Length,byteorder)

To express an integer as a byte array of a specified length

I=int.from_bytes (b ' abc ', ' Big ')

Print (I,hex (i)) #6382179 0x616263

Print (I.to_bytes (3, ' big ') #b ' abc '

Slice

Linear structure ( iterative, can be used Len to take length, can be accessed by subscript), you can slice

Access a piece of data for a linear structure through an indexed interval

Sequence[start,stop] Represents a subsequence that returns a [Start,stop] interval

[:] means from beginning to end, equivalent to copy ()

Step slices

[Start:stop:step]

Step step, can be plus or minus integer, default 1

Step is to be start:stop with the same direction, otherwise an empty sequence is returned

Chapter. 2.2, Python built-in data structure: strings, bytes, and byte arrays

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.