“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Socket programming:
- The bind, recvfrom, and sendto function is similar to c except that it does not return a value to tell when an error occurs. Instead, it raises an exception.
- Sockets are also closed for garbage collection.
- Obtain the IP address of the nic:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack('256s', ethname[:15]))[20:24])
Copy the code
Second, the use of strings
- Python strings cannot be changed. But you can manipulate strings to form new strings.
- Removes a string from a string. This method is not provided directly, but replace can implement:
"abc def".replace(" ", "")
Copy the code
There is another method for the same function: translate. It replaces one character in a string with another character, not a string. Its first argument is a conversion table. The second argument is the string to delete. We can do this with the second parameter, del. Also, the first argument is set to None.
Translate might be more efficient. In addition, its second argument can make a string containing more than one character, which will delete more than one.
Note: the Translate method does not operate on this string, but returns a new string.
-
Strip method: Removes Spaces on both sides of the string and returns a new string. This feature is very useful.
-
There is a function in STR, format, which is very powerful, and you should take a look at it sometime.
-
Endswitch: Checks whether a string ends. Startswith: Checks whether a string has already started.
-
Partition: Divides the string into three parts by the specified string and returns a tuple. The first is to specify what comes before the string, the second is to specify the string, and the third is to specify what comes after the string. It’s great for string parsing.
-
Split: Splits a string into substrings based on a specified string and returns a split list.
-
Join: Joins various strings in a list of strings, inserting the specified string in between.
-
Find does not return false or true, so it cannot be used directly for if judgments. If s.type (‘ ‘) >= 0:
-
Dictionary-based formatting:
- sh = ‘ ‘ ‘
- python -m compileall -fl .. /src;
- python -m compileall -fl .. /src/micbase;
- mkdir %(packname)s;
- mdkir %(packname)s;
- ‘ ‘ ‘ % { ‘packname’ :sys.argv[1], }
- print(sh)
Built-in functions:
string.capitalize() | Capitalize the first character of the string |
string.center(width) | Returns an original string centered and filled with Spaces to a new string of length width |
string.count(str, beg=0, end=len(string)) | Return the number of occurrences of STR in string, if beg or end means the number of occurrences within the specified range |
string.decode(encoding=’UTF-8′, errors=’strict’) | Decodes strings in the encoding format specified for encoding, and defaults to ValueError unless errors specify ‘ignore’ or ‘replace’ for errors |
string.encode(encoding=’UTF-8′, errors=’strict’) | String is encoded in the encoding format specified for encoding. If an error occurs, ValueError is raised by default, unless errors specifies ‘ignore’ or ‘repl ‘ |
string.endswith(obj, beg=0, end=len(string)) | Check if the string ends in obj, if beg or end specifies obj, True if so, Fa otherwise |
string.expandtabs(tabsize=8) | Convert the TAB character in string to a space. The default tabsize is 8. |
string.find(str, beg=0, end=len(string)) | Check if STR is included in string, if beg and end specify the norm, and -1 if it is the starting index |
string.index(str, beg=0, end=len(string)) | Same as the find() method, except that an exception is reported if STR is not in string |
string.isalnum() | A, b, c R Return True if string has at least one character and all characters are alphanumeric, False otherwise |
string.isalpha() | A, B, c Return T if string has at least one character and all characters are letters otherwise False |
string.isdecimal() | B, C, d Return True if string contains only decimal digits; False otherwise. |
string.isdigit() | B, c Return True if string contains only numbers, False otherwise. |
string.islower() | B, c Return True if the string contains at least one case sensitive character, and all of these characters are lowercase, otherwise return False |
string.isnumeric() | B, C, d Return True if string contains only numeric characters, False otherwise |
string.isspace() | B, c Return True if string contains only Spaces, False otherwise. |
string.istitle() | B, c Return True if string is titled (see title()), False otherwise |
string.isupper() | B, c Return True if the string contains at least one case-sensitive character and all of these characters are uppercase, False otherwise |
string.join(seq) | Merges (concatenates) Merges all the elements in SEQ into a new string, using string as the delimiter |
string.ljust(width) | Return a left-aligned string and fill it with Spaces to a new string of length width |
string.lower() | Converts all uppercase characters in string to lowercase. |
string.lstrip() | Truncate the space to the left of string |
string.partition(str) | Somewhat like a combination of find() and split(), e splits the string into a three-element tuple (string_pre_str, STR,string_post_str) from the first occurrence of STR, if If string does not contain STR, string_pre_str == string. |
string.replace(str1, str2, num=string.count(str1)) | Replace str1 in string with str2, not more than num, if num is specified. |
string.rfind(str, beg=0,end=len(string)) | Similar to find(), but starting from the right. |
string.rindex( str, beg=0,end=len(string)) | Similar to index(), but starting on the right. |
string.rjust(width) | Return a right-aligned string filled with Spaces to the new string of length width |
string.rpartition(str) | E is similar to partition(), but looks up from the right. |
string.rstrip() | Delete the space at the end of the string. |
string.split(str=””, num=string.count(str)) | String is sliced with STR as the delimiter. If num has a specified value, only num substrings are delimited |
string.splitlines(num=string.count(‘\n’)) | B, c, return a list of rows as elements, if num is specified, only num rows are sliced. |
string.startswith(obj, beg=0,end=len(string)) | B, e checks if the string begins with obj and returns True if it does, and False otherwise. If beg and end specify values, check within the specified range. |
string.strip([obj]) | Execute lstrip() and rstrip() on string |
string.swapcase() | Invert case in string |
string.title() | B, c return “titled” strings, meaning that all words start in uppercase and all other letters are lowercase (see istitle()). |
string.translate(str, del=””) | Convert string characters from the table (256 characters) given by STR, and place the characters to be filtered into the del argument |
string.upper() | Converts lowercase letters in string to uppercase letters |
string.zfill(width) | Returns a string of length width, right-aligned with string, preceded by 0 |
Regular expressions
- Why you learn regex: The main reason for learning regex is to make it easier to manipulate strings, especially as a reserve for later code generation.
- Match is a match at the beginning of the string, and search is a match at any starting position of the string.
- Sub can replace parts of a string that match patterns
- Split: A string can be split, in this case by pattern.