A comprehensive understanding of python strings and dictionaries, and a comprehensive understanding of python

Source: Internet
Author: User

A comprehensive understanding of python strings and dictionaries, and a comprehensive understanding of python

Many serial method strings are also applicable,
However, the string is immutable, so some methods that try to change the string are not available.

1 string formatting

1) format the string with tuples or dictionaries

Format = "hello, % s. s % enough for you? "
Values = ('World', 'hot ')
Format % values

Similar to C format

2) template string

The string module provides template strings to format strings.
From string import Template
S = Template (x, gloriousx, gloriousx !)
S. substitute (x = 'slurm ')
Replace x with slurm
Some detailed tips and usage


Format Conversion Type, field width precision, symbol bit, alignment, fill, etc.


2 string method

1) find

Returns the leftmost index.
S. find (subs)

2) join
Connection string

3) lower

4) replace
All matching items are replaced

5) split
The separator of the split string does not contain
.........
..........

--------------------------------------------------

The values in the dictionary do not have a specific order.

The key can be a number, string, or tuples (The key must be of an unchangeable type, not a list)

Phonebook = {'jmz': '000000', 'usr1 ': '000000', 'usr2': '000000 '}

1) dict Functions

Create a dictionary using other mappings or key-value sequences:
Use a list containing two tuples to create a dictionary:

Items = [('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3 ')]
D = dict (items)
{'Key1': 'value1', 'key2': 'value2', 'key3': 'value3'} is not in this order.

Create a dictionary Using Keyword parameters:
D = dict (key1 = 'value1', key2 = 'value2', key3 = 'value3 ')
The above results will also be obtained.

The dict function is not a real function. It is a type, similar to list, tuple, and str.

2) basic dictionary operations

Assume that d is a dictionary:
Len (d) dictionary Length
D [key] key value in the dictionary
D [key] = value assignment (automatically added if no key exists)
Del d [key] deletes an item whose key is key.
Check whether the key in d is in the dictionary

3) The dictionary can also be used for formatting.

Add a key (enclosed in parentheses) after each conversion specifier (%) to keep up with other specifiers:
Example: % (value) s
Phonebook = {'jmz': '000000', 'usr1 ': '000000 '}
"Jmz's phone number is % (jmz) s." % phonebook
In this way, as long as the given key can be found in the dictionary, any number of conversion specifiers can be obtained.

4) dictionary Methods
Clear:
In-situ operation (no return value) to clear all items in the dictionary

Copy: Light copy and deep copy
Y = x. copy ()
Y = deepcopy (x)

Fromkeys:
Use the given key to create a new dictionary. The default value is None.
>>>{}. Fromkeys (['key1', 'key2'])
>>>{ 'Key1': None, 'key2': None}
Or:
>>> Dict. fromkeys (['key1', 'key2'])
Same effect

Get:
In general, Dictionary items with good Fan Wen may go wrong, for example:
>>> Print d [name]
While
>>> Print d. get ('name') returns None by default.

Has_key:
Returns True or False.
>>> D. has_key ('jmz ')

Items and iteritems:
The items method returns the dictionary items as a list:
D = {'key1': 'value1', 'key2': 'value2 '}
>>> D. items ()
>>> [('Key1', 'value1'), ('key2', 'value2')]
Iteritems returns iteration objects for items.
>>> It = d. iteritems () # it is the iterator object of the above list
>>> List (it) # You can convert the iterator to a list.
>>> [('Key1', 'value1'), ('key2', 'value2')]
 
Keys and iterkeys:
Returns the key in the form of a list. The iterator of the key

Pop:
D. pop ('key') has a return value and is removed.

Popitem:
D. popitem () pops up a random project because the dictionary is unordered.

Setdefault:
>>> D. setdefault ('key', 'default _ value ')
If the key does not exist, the default value is returned and the dictionary is updated. If the key already exists, its value is returned. If the dictionary is not updated, this statement does not work.

Update:
Update another dictionary using one dictionary:
>>> D. update (dd)
Add the items in dd to d. If the items are repeated, overwrite them.

Values and itervalues:
List of returned values, the iterator of the Value List.

The above comprehensive understanding of python strings and dictionaries is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the help house.

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.