Python Learning Note 2: List, dictionary, string manipulation

Source: Internet
Author: User

A module

A module is a code that someone else has written and can be referenced by an import. A standard library is a library that is officially released by Python and is installed by default when you install Python.

1.os

and system-related, such as:

>>> Os.system ("LS-LRTHF")
Total 20M
-rw-r--r--. 1 root root 20M Jul 13:48 python-3.5.2.tgz
-RW-------. 1 root root 958 Jul 19:34 anaconda-ks.cfg
Drwxrwxr-x. 4.0K Jul 23:36 python-3.5.2/

>>> os.path.isfile ("/root")
False
>>> Os.path.isdir ("/root")
True

Two lists

name = ["NAME0", "name1", "name2"]

1. Index Operations:

>>> Name[0]
' NAME0 '
>>> Name[1]
' Name1 '

The list is sequential.

2. Shard Operation:

>>> Name[0:2]
[' NAME0 ', ' name1 ']
>>> Name[-1]
' Name2 '
>>>

When you don't know the length of the list, 1 is a good choice.

3. Append operation:

>>> name.append ("Name3")
>>> Name
[' NAME0 ', ' name1 ', ' name2 ', ' Name3 ']
>>>

4. Delete the element:

>>> name.remove ("name2")
>>> Name
[' NAME0 ', ' name1 ', ' Name3 ']
>>> Name.pop ()
' Name3 '
>>> Name
[' NAME0 ', ' name1 ']
>>>

The Remove method deletes the specified element, the Pop method deletes the last element, and returns the element value

5. Modify the element:

>>> Name
[' NAME0 ', ' name1 ', ' name2 ']
>>> name[2]= "Name6"
>>> Name
[' NAME0 ', ' name1 ', ' Name6 ']
>>>

6. Membership: In

>>> name = ["NAME0", "name1", "name2"]
>>> "Name1" in Name
True
>>> "Name5" in Name
False
>>>

Three dictionaries

Dictionaries are key-value pairs, and k-v pairs are unordered, so there is no index operation, by using key to access value values, the key value is equivalent to the index, cannot be duplicated

>>> dic={"name": "Shirley", "Age": "+", "job": "Teacher", "country": "China"}
>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' age ': ' + ', ' job ': ' Teacher '}

1. Increase

>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' age ': ' + ', ' job ': ' Teacher '}
>>> dic["School"]= "Qinghua"
>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' age ': ' + ', ' job ': ' Teacher ', ' school ': ' Qinghua '}
>>>

2. Modifications

>>> dic["School"]= "Beida"
>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' age ': ' + ', ' job ': ' Teacher ', ' school ': ' Beida '}
>>>

3. Delete

>>> Dic.pop ("Age")
' 26 '
>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' job ': ' Teacher ', ' school ': ' Beida '}
>>>

>>> del dic["job"]
>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' School ': ' Beida '}
>>>

Del is a universal function

4. Get the value

>>> DIC
{' Country ': ' China ', ' name ': ' Shirley ', ' age ': ' + ', ' job ': ' Teacher ', ' school ': ' Qinghua '}
>>> dic.get ("name")
' Shirley '
>>> dic["Name"]
' Shirley '
>>> dic.get ("lll")
>>> DiC. ["LLL"]
File "<stdin>", line 1
Dic. ["LLL"]
^
Syntaxerror:invalid syntax
>>>

Get returns None when no key is used, and an error occurs when using the index operation.

6. Memberships

>>> "Name" in DIC
True
>>>

7. Cyclic DIC

info={' 1 ': "100", "2": "200", "3": "300", "4": "400", "5": "500"}
For key in info:
Print (Key,info[key])

5 500
4 400
3 300
1 100
2 200

8. Keys () method

>>> Dic.keys ()
Dict_keys ([' Country ', ' name ', ' age ', ' job ', ' school ')

Returns a key as a list

Four-string method

A.format_map () #传入的参数是字典

A.index () #同find () method

A.isalnum () #检测字符串是否由字母和数字组成

A.isalpha () #检测字符串是否只由字母组成

A.isdigit () #判断字符串是不是数字

A.isidentifier () #判断字符串是否是合法的标识符

A.islower () #判断是不是小写

A.isnumeric () #检测字符串是否只由数字组成

A.isspace () #判断字符串是否仅包含空格或制表符. Note: space characters are different from whitespace

A.istitle () #判断字符串是不是标题格式

A.isupper () #判断是不是大写

A.join () #连接字符串数组. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection

A.ljust () #返回一个原字符串左对齐 and fills a new string of the specified length with a space.

A.lower () #把大写转成小写

A.lstrip () #去掉字符串左边的空格

A.maketrans () #将左右参数对应, replace the left argument with the right argument

A.replace () #把字符串中的 old (older string) replaced with new

A.rfind () #返回字符串最后一次出现的位置, returns 1 if no match is found

A.rindex () #返回子字符串 the last occurrence of str in a string

A.rjust () #返回一个原字符串右对齐 and fills a new string with a space to a specified length

A.rsplit () #去掉右边空格或换行符

A.rstrip () #去掉字符串右边的空格

A.strip () #去掉字符串中的空格

A.split () #字符串切片

The A.splitlines () #按行分隔 Returns a list that contains rows as elements, and if NUM specifies that only num rows are sliced.

A.startswith () #判断是不是以参数中内容开头

A.swapcase () #对字符串的大小写字母进行转换

A.title () #把字符串转成标题格式

A.capitalize () #把字符串首字母变为大写

A.endswith () #判断字符串是否以指定字符或子字符串结尾, commonly used to determine file types

A.expandtabs () #把字符串中的 the tab sign (' \ t ') to a space, returning the new string generated after the tab symbol (' \ t ') in the string is converted to a space

A.format () #格式化字符串

A.find () #返回参数在字符串中首次出现的位置

Python Learning Note 2: List, dictionary, string manipulation

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.