This article mainly describes the Python string and dictionary-related operations of the example of the relevant information, here to provide examples to help you understand this part of the content, the need for friends can refer to the following
Examples of Python string and dictionary-related operations
String manipulation:
The% format operation of the 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## A single variable replace S1 = Template (' $x, glorious $x! ') Print S1.substitute (x = ' Slurm ') # # # dollar sign and the substitution of a single variable s2 = Template ("Make $$ selling $x!") Print S2.substitute (x = ' Slurm ') # # field variable substitution 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.
Type of string formatting:
(1)% character: The beginning of the token conversion specifier is the beginning of the substitution.
(2)-Indicates left alignment, + indicates that the value is preceded by a positive sign. 0 indicates that the number of converted values is not enough to be populated with 0.
(3) * You can specify the minimum field width.
(4) point (.) followed by the precision value.
String method:
(1) Find: You can find substrings in a longer string and return to the leftmost index where the substring is located. Returns 1 if it is not found.
print ' With a moo-moo here, and a moo-moo there '. Find (' Moo ') return: 7
(2) Join method: Strings are stitched together.
print '/'. Join ((' ', ' usr ', ' bin ', ' env ')) output:/usr/bin/envps: And Google's guava a bit like.
(3) Lower method: Returns the lowercase master of a string.
print ' AK47 '. Lower () output: AK47
(4) Replace method: Returns the resulting string after all occurrences of a string have been replaced.
' This was a test '. Replace (' is ', ' Ezz ') output: Thezz Ezz a test
(5) Split method: The inverse method of join, separating the string into a sequence.
print ' 1+2+3+4+5 '. Split (' + ') output: [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
(6) Strip method: Remove both sides of the string, the default is a space string, you can also specify the corresponding string.
PS: You can also participate in Lstrip and Rstrip methods.
(7) Translate method: As with the Replace method, some parts of the string can be replaced, but unlike the former, the translate method only handles a single character. It has the advantage of being able to make multiple substitutions at the same time, sometimes more efficiently than replace.
The Ps:maketrans method is similar to the Translate method.
Basic operation of the dictionary:
(1) Dict method: Used to construct dictionary data.
Dict (name= ' Gumby ', age=42) dict ([(' Name ', ' Gumby '), (' Age ', p)]) PS: Are all methods of constructing a field.
(2) Dictionary basic operation:
1. Len (d) Returns the number of items (key values) in D.
2. D[k] Returns the value associated to the key K.
3. D[k]=v associates The value V to the key K.
4. del D[k] Delete the key to K.
5. K in D checks if D contains an entry with a key of K.
(3) The Copy method returns a new dictionary with the same key-value pair.
(4) Fromkeys: Method uses the given key to create a new dictionary, each key corresponding to the value of none.
print {}.fromkeys ([' Name ', ' age ']) output: {' age ': none, ' Name ': none}
(5) Get method: The Get method is a more relaxed dictionary entry method.
D = {}d[' name '] will have an error when this is accessed. When D.get (' name ') is accessed, none will be returned if it does not exist.
(6) The Haskey:haskey method can check whether the dictionary contains the given key. D.has_key (k) corresponds 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 in no order. Iteritems and items are similar, but the iterator is returned.
(8) Keys and Iterkeys and item are similar, this is the list or iterator that returns key.
(9) The values method returns the value in the dictionary as a list, and unlike keys or Iterkeys, the returned value can contain duplicate values.
The Update method can use a dictionary to update another dictionary.