This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money. Small knowledge, big challenge! This article is participating in the “Essentials for Programmers” creative activity. This article has participated in the “Digitalstar Project” to win a creative gift package and challenge the creative incentive money. Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
A string
1.1 Definition of Strings
A string is an “ordered” sequence of characters, such as single quotes, double quotes, and triple quotes.
str='hello'
str="hello"
str="""hello
"""
Copy the code
1.2 Subscript and slice
1.2.1 Subscript index
The concept of subscripts in Python refers to numbers. Subscripts are often used in lists of string tuples, and we can find their elements by subscripts. For example, we created a string name = zhang and now want to fetch the a character in the string named name. How do I get it? We can retrieve characters from the string by iterating through the for loop, but Python gives us an easier way to retrieve a characters with subscripts
1.2.2 slice
We can use the following table index to retrieve the corresponding element in the string, but if we want to intercept an element, we need to slice. Slicing is the operation of taking a portion of the object being operated on. Strings, lists, and tuples all support slicing. The syntax for slicing is as follows: [Start: end: step] The following uses a string as an example. If part of it is taken out, then in brackets [], use:
1.2.3 Common Operations on Strings
If there is a string mystr=’hello world everyone’, there are the following common operations: 1, find checks whether STR is contained in a string, returns the starting index if it is, and -1 otherwise
2. Index is the same as find () except that an exception is raised if STR is not in mystr.
3, count returns the number of occurrences of STR in mystr between start and end
4, replace Replaces str1 in mystr with str2, no more than count if count is specified.
5, split splits mystr with STR as the delimiter. If maxsplit has a specified value, only maxsplit substrings are split
Capitalize the first character of a string
7, title
8, startswith checks if the string begins with STR, and returns True if it does, and False otherwise
9, endswith checks if the string ends in obj and returns True if so, False otherwise.
Lower converts all uppercase characters in mystr to lowercase 11 and upper converts lowercase characters in mystr to uppercase
12, rjust returns a right-aligned string and fills it with space to the new string 13 of length width, ljust returns a left-aligned string and fills it with space to the new string 14 of length width, Center returns an old string centered and fills it with Spaces to a new string 15 of length width. Lstrip removes the whitespace character to the left of mystr
Strip removes whitespace at the end of mystr. 17 Strip removes whitespace at both ends of mystr. Rfind is similar to find(), but starts from the right.
19. Rindex is similar to index(), but starts on the right.
Partition divides mystr into three parts with STR before, STR, and STR after
21. Rpartition is similar to partition(), but starts on the right.
22, Splitlines returns a list of rows as elements, separated by rows
23, insert STR after each element in join mystr to construct a new string
The second list
2.1 Introduction to the List
One data type built into Python is list :list. (A list is an ordered collection of values whose elements can be added and removed at any time, written between square brackets and separated by commas. All items in a list need not be of the same type. What’s more powerful than arrays in C is that the elements in a list can be of different types.
2.1.1 Length of the list
2.1.2 List access
To access elements at each position in the list, use indexes starting at 0 :(left to right), indexes starting at -1, (right to left)
2.1.3 Section of the list
Get a piece of data based on the range of subscripts, such as a list, or a string using slicing. [Start subscript: End subscript: step] Tip: The default start subscript is 0, the default end subscript is not included, and the default step is 1
2.1.4 Adding elements (Append, extend, INSERT)
1, append
2, extend
3, insert
2.1.5 Modifying elements
When you modify an element, you determine which element you want to modify by subscript, and then you can modify it
2.1.6 Finding Elements
A lookup is to see if the specified element exists.
2.1.7 Deleting Elements
Common methods for deleting list elements are: del: deletes the last element based on the subscript pop: deletes the last element based on the value of the element remove: deletes the element based on the value of the element
2.1.8 sorting
The sort method rearranges the list in a specific order. The default is from small to large, and reverse=True can be changed to reverse from large to small.
A triple
Another kind of ordered list is called a tuple: a tuple. A tuple is very similar to a list, but a tuple cannot be modified once it is initialized.
- Now, the tuple of the classmates cannot be changed, and it does not have append(), insert() and other methods. The method of obtaining other elements is the same as that of list. You can normally use classmates[0], classmates[-1], but cannot assign to other elements.
- What’s the point of an immutable tuple? Because tuples are immutable, the code is safer. If possible, use tuples instead of lists.
- To define an empty tuple, write ():
- A tuple with only one element must be defined with a comma to disambiguate it: parentheses can represent both tuples and parentheses in mathematical formulas.
Four dictionary
Dictionaries are another mutable container model that can store objects of any type. Each key=value pair of the dictionary is separated by a colon (:), each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces
A key corresponds to only one value. If you add a value to a key multiple times, subsequent values will flush out previous values. Dict reports an error if the key does not exist
4.1 Common Dictionary Operations
4.1.1 Modifying elements
4.1.2 Adding Elements
If the “key” does not exist in the dictionary when the variable name [” key]= data is used, the element is added. Add new elements
4.1.3 Deleting Elements
To delete a dictionary, you can:
- del
- clear ( )
Del Deletes the specified element
Clear Clears the entire dictionary
4.1.4 Measure the number of key-value pairs in the dictionary
4.1.5 keys
Returns a list of all the keys in the dictionary
4.1.6 values
Returns a list of all the dictionary values
4.1.7 the items
Returns a list of all (key, value) primitives
Five set
A set is an unordered sequence of non-repeating elements. You can use curly braces or the set() function to create a collection. Note: To create an empty collection, you must use set() instead of {}, because {} is used to create an empty dictionary.
Collections can de-duplicate container type data
Lists, tuples, and sets can be converted to each other.