Python clock in the fourth day, list, tuple

Python check in on the fourth day, list, tuple
operation problem is not finished, continue tomorrow

List

The list is a basket, you can put everything in it. The elements can be of different types

list.append(obj) Add a new object at the end of the list

list.extend(seq) Append multiple values ​​from another sequence at the end of the list at once

list.insert(index, obj)The number indexinsertion position obj.

list.remove(obj) Remove the first occurrence of a value in the list

list.pop([index=-1]) Remove an element from the list (the last element by default), and return the value of the element

Tuples and lists no difference, except that the tuple is created after can not be modified, so only countand indexboth methods. , Can also be spliced

Strings have some built-in operations

  • capitalize() Convert the first character of the string to uppercase.

  • lower()` Convert all uppercase characters in the string to lowercase.

  • upper() Convert lowercase letters in the string to uppercase.

  • swapcase() Convert uppercase to lowercase and lowercase to uppercase in a string.

  • count(str, beg= 0,end=len(string))Returns strthe number of occurrences in string, begor if endspecified, returns strthe number of occurrences in the specified range .

  • endswith(suffix, beg=0, end=len(string)) 检查字符串是否以指定子字符串suffix 结束,如果是,返回 True,否则返回 False。如果beg end` If the specified value is specified, it will be checked within the specified range.

  • startswith(substr, beg=0,end=len(string))Check whether the specified string substring substrbeginning, and if so, returns True, otherwise False. If it begand endthe specified value, it is checked in the specified range.

  • find(str, beg=0, end=len(string))Detecting strwhether or not contained in the string, if the specified range begand end, it is checked whether contained within the specified range, if included, return the index value starting, otherwise -1.

  • rfind(str, beg=0,end=len(string))Similar find()function, but start looking from the right.

  • isnumeric() If the string contains only numeric characters, it returns True, otherwise it returns False.

  • ljust(width[, fillchar])Return a new string fillcharwith the original string left-justified and padded to the length with (default spaces) width.

  • rjust(width[, fillchar])Return a new string fillcharwith the original string right-aligned and padded to the length with (default spaces) width.

【example】

str4 = '1101'
print(str4.ljust(8, '0'))  # 11010000
print(str4.rjust(8, '0'))  # 00001101
  • lstrip([chars]) Truncate the spaces or specified characters on the left side of the string.

  • rstrip([chars]) Delete spaces or specified characters at the end of the string.

  • strip([chars])Perform lstrip()and on the string rstrip().

  • partition(sub)Find the substring sub, divide the string into a triple (pre_sub,sub,fol_sub), and return if the string does not contain sub ('原字符串','','').

  • rpartition(sub)Similar to the partition()method, but search from the right.

  • replace(old, new [, max])Replace the in the string oldwith new, if maxspecified, the replacement will not exceed maxtimes. format

String formatting symbol

symbol description
%c Formatting characters and their ASCII codes
%s Format the string, use the str() method to process the object
%r Format string, use rper() method to process object
%d Format integer
%The Format an unsigned octal number
%x Format an unsigned hexadecimal number
%X Format an unsigned hexadecimal number (uppercase)
%f Format floating point numbers, you can specify the precision after the decimal point
%e Format floating point numbers in scientific notation
%E Same function as %e, format floating point number in scientific notation
%g Decide to use %f or %e according to the value
%G The effect is the same as %g, according to the value of the decision to use %f or %E
  • Format operator auxiliary instructions
symbol Features
m.n m is the minimum total width of the display, n is the number of digits after the decimal point (if available)
- Used for left alignment
+ Show plus sign (+) in front of positive numbers
# Display zero ('0') in front of the octal number, display '0x' or '0X' in front of the hexadecimal number (depending on whether you are using'x' or'X')
0 Fill the number displayed with '0' instead of the default space

Guess you like

Origin blog.csdn.net/sinat_39470268/article/details/107646752