Examples of operations related to Python strings and dictionaries, and python strings
Examples of operations related to Python strings and dictionaries
String operation:
% Formatting of string:
str = "Hello,%s.%s enough for ya ?"values = ('world','hot')print str % values
Output result:
Hello,world.hot enough for ya ?
Template string:
# Coding = utf-8from string import Template # Replace a single variable s1 = Template ('$ x, glorious $ x! ') Print s1.substitute (x = 'slurm') # dollar sign representation and replacement of a single variable s2 = Template ("Make $ selling $ x! ") Print s2.substitute (x = 'slurm') # Replace the field variable s3 = Template ('A $ thing must never $ action. ') d = {} d ['thing'] = 'gentleman 'd ['action'] = 'show his socks' print s3.substitute (d) ps: safe_substitute does not cause errors due to missing values or incorrect use of the $ character.
String formatting type:
(1) % character: mark the start of the conversion specifier, that is, start to replace.
(2)-indicates the left alignment, and + indicates adding positive and negative signs before the conversion value. 0 indicates that if the number of digits of the conversion value is not enough, fill it with 0.
(3) * The minimum field width can be specified.
(4) Point (.) follows the precision value.
String method:
(1) find: You can search for a substring in a long string and return the leftmost index of the substring. If not found,-1 is returned.
Print 'With a moo-moo here, and a moo-moo there'. find ('moo') returns: 7
(2) join method: concatenates strings.
Print '/'. join ('', 'usr', 'bin', 'env') Output:/usr/bin/envps: A bit like Google's guava.
(3) lower method: returns the lower-case string.
Print 'ak47'. lower () Output: AK47
(4) replace method: returns the string obtained after all matching items of a string are replaced.
'This is a test'. replace ('is ', 'ezz') Output: Thezz ezz a test
(5) split method: inverse join method, which separates strings into sequences.
Print '1 + 2 + 3 + 4 + 5 '. split ('+') Output: ['1', '2', '3', '4', '5']
(6) strip Method: remove the strings on both sides. By default, it is a space string. You can also specify the corresponding string.
Ps: You can also use the lstrip and rstrip methods.
(7) translate method: similar to the replace method, some parts of the string can be replaced, but unlike the former, the translate method only processes a single character. Its advantage is that it can be replaced at the same time, sometimes more efficient than replace.
Ps: The maketrans method is similar to the translate method.
Basic dictionary operations:
(1) dict method: used to construct Dictionary data.
Dict (name = 'gumby', age = 42) dict ([('name', 'gumby'), ('age', 42)]) ps: both are methods for constructing fields.
(2) Basic dictionary operations:
1. len (d) returns the number of items (key value) in d.
2. d [k] returns the value associated with the key k.
3. d [k] = v associates the value v with the key k.
4. del d [k] Delete the key k.
5. k in d check whether d contains keys k.
(3) The copy method returns a new dictionary with the same key-value pairs.
(4) fromkeys: The method creates a new dictionary using the given key. The value of each key is None.
Print {}. fromkeys (['name', 'age']) Output: {'age': None, 'name': None}
(5) get method: The get method is a more loose dictionary item method.
D = {} d ['name'] returns an error when you access the service. D. When you access get ('name'), None is returned if it does not exist.
(6) The haskey: haskey method can check whether the dictionary contains the given key. D. has_key (k) is equivalent to k in d.
(7) items and iteritems methods:
The items method returns the dictionary in the form of a list of key-value tuples, but there is no order. Iteritems is similar to items, but an iterator is returned.
(8) keys and iterkeys are similar to item. This is the list or iterator that returns the key.
(9) The values method returns the value in the dictionary in the form of a list. Unlike keys or iterkeys, the returned value can contain duplicate values.
(10) the update method can be used to update another dictionary.
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!