Python data Structure-string

Source: Internet
Author: User

The string is the most commonly used data type in Python. We can use single quotation marks (') or double quotation marks ("") to create a string.

In Python, creating a variable does not require a specified type, and when you create a variable assignment, the Python interpreter automatically infers the variable's data type

1>>> s ='ABCDE'2>>>type (s)3<class 'Str'> #这里把多种数据类型的值赋值给s, Python can automatically identify4>>> s = 1235>>>type (s)6<class 'int'>7>>> s = [1, 2,'a', 3]8>>>type (s)9<class 'List'>Ten>>> s = {'name':'Root'} One>>>type (s) A<class 'Dict'> ->>> s = {1, 3,'a','Abd'} ->>>type (s) the<class 'Set'> ->>>

Single character types are not supported in Python, and single characters in Python are also handled as strings

In Python, because the string is an ordered sequence, operations such as indexing, slicing, and so on (using slices to get substrings) are supported.

1>>> s ='a'2>>>type (s)3<class 'Str'> #python中单字符也是字符串4>>> s ='ABCDE'5>>> S[0:3] #字符串可以切片, get the substring6 'ABC'7>>> s[1] #字符串可以索引8 'b'9>>>'BC' inchs #字符串支持 in, not in operationTen True One>>> forIinchs: #字符串可以循环 A     Print(i) -  -      the a - b - C - D + e ->>>

In Python, in order to prevent each creation of the underlying variables, such as strings, numbers, etc. to apply for memory, so Python has a dedicated memory pool, storing the usual numbers, strings and so on. So in Python, data types such as numbers, strings, and so on are ' immutable ',

The Re-assignment (change) is equivalent to pointing the variable name to another piece of memory in memory.

1>>> s ='ABC def wsx ASD Hello'2>>>ID (s)330632919675044>>> S1 = s.replace ('def','XXX')5>>>s6 'ABC def wsx ASD Hello'7>>>S18 'abcxxx wsx asd Hello' #字符串本身不能更改, the Re-assignment (change) is equivalent to pointing to another piece of memory9>>>ID (S1)Ten3063291967720 One>>> i = 10 A>>>ID (i) -1554097536 ->>> i = #数字不能重新赋值 (change), re-change is equivalent to pointing to a piece of memory the>>>ID (i) -1554097856 ->>>

So in Python, when you update a string, you have to re-open the memory space

1>>> s ='Hello, Tom .'2>>> S1 = s[:6]+'Lucy' #更新字符串, not recommended when stitching strings with ' + ', ' * ' means duplicate output string, eg: ' abc '---' abcabcabc '3>>>S14 'Hello Lucy'5>>>ID (s)630633060414567>>>ID (S1)830633060413929>>>

Python uses a backslash (\) to escape characters when special characters are needed in characters. As the following table:

\ (at end of line) Line continuation character
\\ Backslash symbol
\‘ Single quotation marks
\" Double quotes
\a Bell
\b BACKSPACE (Backspace)
\e Escape
\000 Empty
\ n Line break
\v Portrait tab
\ t Horizontal tab
\ r Enter
\f Page change
\oyy Octal number, the character represented by YY, for example: \o12 for newline
\xyy Hexadecimal number, the character represented by YY, for example: \x0a for line break
\other Other characters are output in normal format

Note that the original string is printed in front of the string and r/r. That is, all strings are used directly in the literal sense, without escaping special or non-printable characters.

The original string has almost exactly the same syntax as a normal string, except that the first quotation mark of the string is preceded by the letter "R" (which can be case).

Formatting of strings:

Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.

1>>> s ='You is my%s I am%s year old'%('Sunshine', 22)2>>>s3 'You are my Sunshine I am'4>>> S1 ='You is my{} I am {} year old'. Format ('Sunshine', 22)5>>>S16 'You are mysunshine I am'7>>> s2 ='You is my{1} I am {0} year old'. Format (22,'Sunshine')8>>>S29 'You are mysunshine I am'Ten>>> s3 ='You is my {what} I am {age}'. Format (age=22,what='Sunshine') One>>>S3 A 'You are my Sunshine I am' ->>>

Three quotes in Python ("'") or ("" "" ""):

Python three quotes allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters.

The syntax for a triple quotation mark is a pair of consecutive single or double quotes (usually paired). Can be used to define a string (usually with multiple lines or special symbols), comment code, etc.

1 " " ABC 2         ASD3        \4        \ n5         "'6 >>> s7'  ABC
8 ASD
9 \ \
Ten \ n
One '

String Common methods:

1 #!/usr/bin/env python2 #Coding:utf-83 4 " "String Common methods" "5s ='AbcDEF'6 7 s.capitalize ()8 " "returns the first uppercase copy of the string s" "9S.find (Sub, start=none, end=None)Ten " "returns the index of the first occurrence of a string in the specified interval, without specifying a range representing the entire string, or 1 if it does not exist ." " One S.islower () A " "determine if the characters inside the string are all lowercase" " - S.isupper () - " "determine if the characters inside the string are all uppercase" " the S.lower () - " "returns a copy of all the characters of a string converted to lowercase" " - S.upper () - " "returns a copy of all characters of a string converted to uppercase" " +S.replace (old, new, count=None) - " "returns the full replacement of the substring old in S in the new copy, and if Count is specified, it is replaced only once" " +S.split (Sep=none, maxsplit=_1) A " "returns the substring separated by the SEQ (default space) as a list, if seq= "a", is delimited by "a", Eg:s.split (' a ')" " atS.strip (chars=None) - " "returns a copy of the chars at both ends of the deleted string, with the default chars=none representing the deletion of spaces Eg:s.strip (' a ') to remove "a" at both ends of S" " - s.swapcase () - " "Returns a copy of converting a string to uppercase, uppercase to lowercase" " - s.startswith (x) - " "determines whether the string s starts with x" " in s.endswith (x) - " "determines whether the string s is ending with x" " to s.partition (x) + " "divides the string s by the first encountered X into three parts (' x before ', ' X ', ' X '), and returns in the form of a meta-ancestor" " -S.count (Sub, start=none, end=None) the " "The number of occurrences of seq within a specified interval of [s,e]" "

Not to be continued ...

Python data Structure-string

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.